自定义Log4Net appender无法正确创建表或存储条目

时间:2013-06-03 12:46:10

标签: c# asp.net logging azure log4net

我目前正在创建自定义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);

我很乐意提供任何其他信息,如果有人希望以完整形式查看课程,我们可以打包解决方案。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

在写完你所引用的博客文章之后,我对代码进行了一些小改动。你可以看到我的github repo中的变化:https://github.com/vidarkongsli/azuretablestorageappender

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

我认为这可能有助于这种情况。