我可能会在这里丢失一些基本的东西 - 但是可以在自定义NLog事件中检索HttpContext.Current吗?在自定义NLog目标中检索HttpContext
我试图给每个请求一个独特的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);
}
}