Azure Table 和 log4net
Azure Table and log4net
我创建了将日志保存到 Azure tables 的代码。
我重写 ActivateOptions 方法来创建 table(如果它确实存在)。
public override async void ActivateOptions()
{
base.ActivateOptions();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse
(CloudConfigurationManager.GetSetting("StorageConnectionString"));
_tableClient = storageAccount.CreateCloudTableClient();
await CraeteTablesIfNotExist();
}
private async Task CraeteTablesIfNotExist()
{
CloudTable logCloudTable = _tableClient.GetTableReference(TableName);
await logCloudTable.CreateIfNotExistsAsync();
}
以及将消息保存到 blob 存储的代码:
protected override async void Append(LoggingEvent loggingEvent)
{
try
{
CloudTable cloudTable = _tableClient.GetTableReference(TableName);
TableBatchOperation tableBatchOperation = new TableBatchOperation();
tableBatchOperation.InsertOrReplace(new LogEntry($"{DateTime.UtcNow:yyyy-MM}",
$"{DateTime.UtcNow:dd HH:mm:ss.fff}-{Guid.NewGuid()}")
{
LoggerName = loggingEvent.LoggerName,
Message = loggingEvent.RenderedMessage
});
await cloudTable.ExecuteBatchAsync(tableBatchOperation);
}
catch (DataServiceRequestException drex)
{
ErrorHandler.Error("Couldwrite log entry", drex);
}
catch (Exception ex)
{
ErrorHandler.Error("Exception log entry", ex);
}
}
不行!我不知道为什么,但是如果我将代码从 ActivateOptions 移动到构造函数 table,就会成功。
下面的代码 运行 是我的 ActivateOptions 方法并记录一条消息:
[TestFixture]
public class Log4NetHandler : TableStorage
{
private TableStorage _storage;
[SetUp]
public void Init()
{
_storage = new TableStorage();
_storage.ActivateOptions();
BasicConfigurator.Configure(_storage);
}
[Test]
public void CheckLogger()
{
Append(new LoggingEvent(new LoggingEventData
{
LoggerName = "Taras",
Message = "Message"
}));
}
}
我不明白为什么如果我在 Azure 中 运行 ActivateOptions 方法 table 没有创建?
您可以尝试在没有异步调用的情况下实现 ActivateOptions 方法吗?我的旧代码几乎与您的代码完全相同,但工作正常。
public override void ActivateOptions()
{
base.ActivateOptions();
_account = CloudStorageAccount.Parse(ConnectionString);
_client = _account.CreateCloudTableClient();
_table = _client.GetTableReference(TableName);
_table.CreateIfNotExists();
}
我创建了将日志保存到 Azure tables 的代码。 我重写 ActivateOptions 方法来创建 table(如果它确实存在)。
public override async void ActivateOptions()
{
base.ActivateOptions();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse
(CloudConfigurationManager.GetSetting("StorageConnectionString"));
_tableClient = storageAccount.CreateCloudTableClient();
await CraeteTablesIfNotExist();
}
private async Task CraeteTablesIfNotExist()
{
CloudTable logCloudTable = _tableClient.GetTableReference(TableName);
await logCloudTable.CreateIfNotExistsAsync();
}
以及将消息保存到 blob 存储的代码:
protected override async void Append(LoggingEvent loggingEvent)
{
try
{
CloudTable cloudTable = _tableClient.GetTableReference(TableName);
TableBatchOperation tableBatchOperation = new TableBatchOperation();
tableBatchOperation.InsertOrReplace(new LogEntry($"{DateTime.UtcNow:yyyy-MM}",
$"{DateTime.UtcNow:dd HH:mm:ss.fff}-{Guid.NewGuid()}")
{
LoggerName = loggingEvent.LoggerName,
Message = loggingEvent.RenderedMessage
});
await cloudTable.ExecuteBatchAsync(tableBatchOperation);
}
catch (DataServiceRequestException drex)
{
ErrorHandler.Error("Couldwrite log entry", drex);
}
catch (Exception ex)
{
ErrorHandler.Error("Exception log entry", ex);
}
}
不行!我不知道为什么,但是如果我将代码从 ActivateOptions 移动到构造函数 table,就会成功。 下面的代码 运行 是我的 ActivateOptions 方法并记录一条消息:
[TestFixture]
public class Log4NetHandler : TableStorage
{
private TableStorage _storage;
[SetUp]
public void Init()
{
_storage = new TableStorage();
_storage.ActivateOptions();
BasicConfigurator.Configure(_storage);
}
[Test]
public void CheckLogger()
{
Append(new LoggingEvent(new LoggingEventData
{
LoggerName = "Taras",
Message = "Message"
}));
}
}
我不明白为什么如果我在 Azure 中 运行 ActivateOptions 方法 table 没有创建?
您可以尝试在没有异步调用的情况下实现 ActivateOptions 方法吗?我的旧代码几乎与您的代码完全相同,但工作正常。
public override void ActivateOptions()
{
base.ActivateOptions();
_account = CloudStorageAccount.Parse(ConnectionString);
_client = _account.CreateCloudTableClient();
_table = _client.GetTableReference(TableName);
_table.CreateIfNotExists();
}