gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:01:59 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com