我目前正在创建自定义 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”。结果,记录器失败,表中没有创建任何条目。
以下是我项目的一些摘录:
日志条目.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
}
日志服务上下文.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);
我很乐意提供任何其他信息,并且可以打包解决方案,如果有人希望查看完整形式的类。任何帮助将不胜感激!