繁体   English   中英

Azure表和log4net

[英]Azure Table and log4net

我创建代码以将日志保存到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方法表?

您可以尝试在没有异步调用的情况下实现ActivateOptions方法吗? 我有几乎与您的旧代码完全相同的旧代码,可以正常工作。

public override void ActivateOptions()
{
    base.ActivateOptions();

    _account = CloudStorageAccount.Parse(ConnectionString);
    _client = _account.CreateCloudTableClient();
    _table = _client.GetTableReference(TableName);
    _table.CreateIfNotExists();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM