我创建了将日志保存到azure表的代码。 如果它存在,我会覆盖ActivateOptions方法来创建表。
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移动到构造函数表创建成功。 下面的代码运行我的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方法表没有创建?
答案 0 :(得分:1)
您可以尝试在没有异步调用的情况下实现ActivateOptions方法吗?我的旧代码几乎与你的代码完全相同。
Potato