【发布时间】:2014-03-25 04:53:34
【问题描述】:
我可能在这里遗漏了一些基本的东西 - 但是可以在自定义 NLog 事件中检索 HttpContext.Current 吗?
我试图为每个请求提供一个唯一的 Guid,以便我可以将日志消息与单个事件相关联(即,将单个请求的每个日志事件绑定在一起)。所以,我想将此 Guid 存储在 HttpContext.Current.Items 中,然后在 NLog 目标中检索它并将其包含在日志消息中。
这是我想要访问HttpContext.Current 的示例目标:
[Target("AzureTableTarget")]
public class AzureTableTarget : TargetWithLayout
{
public AzureTableTarget()
{
_appSettings = IoCResolver.Get<IAppSettings>();
}
protected override void Write(LogEventInfo logEvent)
{
var correlationId = HttpContext.Current; //This is always null
var batchOperation = new TableBatchOperation();
CxLogEventBuilder.Build(_appSettings, logEvent).ForEach(batchOperation.Insert);
_loggingTable.ExecuteBatchAsync(batchOperation);
}
}
【问题讨论】:
标签: c# logging nlog httpcontext