自定义Log4Net appender没有正确创建表或存储条目



我目前正在创建一个自定义Log4Net appender的原型,它将在Azure表中存储项目中发生的所有异常的信息。表将基于'LogEntry'类中定义的模型创建。由于这是一个原型web应用程序,目前我已经创建了一个按钮,抛出一个异常来启动记录器,我一直遵循这作为一个指南:

http://www.kongsli.net/nblog/2011/08/15/log4net-writing-log-entries-to-azure-table-storage/

但是,当抛出异常并实例化记录器时,表不会正确创建。而不是基于我的LogEntry类创建表,它只生成(我假设是TableServiceContext默认值)的'PartitionKey', 'RowKey'和'TimeStamp'。结果,日志记录器失败,并且没有在表中创建任何条目。

以下是我项目的一些节选:

LogEntry.cs

public class LogEntry : TableServiceEntity
{
    public LogEntry()
    {
        var now = DateTime.UtcNow;
        // PartitionKey is the current year and month whild RowKey is a combination of the date, time and a GUID.
        // This is so that we are able to query our log entries more efficiently.
        PartitionKey    = string.Format("{0:yyyy-MM}", now);
        RowKey          = string.Format("{0:dd HH:mm:ss.fff}-{1}", now, Guid.NewGuid());
    }
    // This region of the class class represents each entry in our log table.
    #region Table Columns
    ...all columns defined here...
    #endregion
}

LogServiceContext.cs

internal class LogServiceContext : TableServiceContext
{
    public LogServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    internal void Log(LogEntry logEntry)
    {
        AddObject("LogEntries", logEntry);
        SaveChanges();
    }
    public IQueryable<LogEntry> LogEntries
    {
        get
        {
            return CreateQuery<LogEntry>("LogEntries");
        }
    }
}

和从appender类本身提取的内容:

// Create a new LogEntry and store all necessary details.
// All writing to log is done asynchronically to prevent the write slowing down request handling.
Action doWriteToLog = () => {
    try
    {
        _ctx.Log(new LogEntry
        {
            CreatedDateTime         = DateTime.Now,
            UserName                = loggingEvent.UserName,
            IPAddress               = userIPAddress,
            Culture                 = userCulture,
            OperatingSystem         = userOperatingSystem,
            BrowserVersion          = userCulture,
            ExceptionLevel          = loggingEvent.Level,
            ExceptionDateTime       = loggingEvent.TimeStamp,
            ExceptionMessage        = loggingEvent.RenderedMessage,
            ExceptionStacktrace     = Environment.StackTrace,
            AdditionalInformation   = loggingEvent.RenderedMessage
        });
    }
    catch (DataServiceRequestException e)
    {
        ErrorHandler.Error(string.Format("{0}: Could not wring log entry to {1}: {2}",
            GetType().AssemblyQualifiedName, _tableEndpoint, e.Message));
    }
};
doWriteToLog.BeginInvoke(null, null);

我很高兴提供任何额外的信息,并且可以打包解决方案,如果有人希望看到类的完整形式。任何帮助将非常感激!

写完你提到的博文后,我对代码做了一些小的修改。您可以在我的github repo中看到更改:https://github.com/vidarkongsli/azuretablestorageappender

基本上,我所做的是用BeginSaveChanges(SaveChangesOptions)代替SaveChanges()。Batch, null, null)和从AzureTableStorageAppender.Append(LoggingEvent)中删除BeginInvoke语句

我想这可能会有帮助。

最新更新