c# - 自定义 Log4Net 附加程序未正确创建表或存储条目

标签 c# asp.net logging azure log4net

我目前正在创建自定义 Log4Net 附加程序的原型(prototype),该原型(prototype)将在 Azure 表中存储项目内发生的所有异常的信息。该表将根据“LogEntry”类中定义的模型创建。由于这是一个原型(prototype) 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");
        }
    }
}

以及附加程序类本身的摘录:

// 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 存储库中看到更改:https://github.com/vidarkongsli/azuretablestorageappender

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

我认为这可能会有所帮助。

关于c# - 自定义 Log4Net 附加程序未正确创建表或存储条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16897059/

相关文章:

c# - 在 .NET 中动态添加引用

logging - 记录 Laravel 中的操作和执行时间

linux - Apache - 最后一小时的日志

c# - 使用 RollingFileAppender 时出现 Log4Net 问题

c# - 如何使用属性的依赖注入(inject)?

c# - Outlook 文件夹浏览对话框?

c# - 如何连接 Debug.Writeline() 和 Debug.Write() 的替代目的地

asp.net - safari 浏览器无法处理基于 cookie 的 asp.net session

c# - 分别访问asp.net GridView中每行的文本框和下拉列表

logging - Elasticsearch日志说明