如何解决NLog,访问自定义目标中的布局渲染器
我正在创建一个自定义目标,但我发现无法从 Write 方法访问布局渲染器值,例如 aspnet-traceidentifier https://github.com/NLog/NLog/wiki/AspNetTraceIdentifier-Layout-Renderer。
这是我使用的代码:
[Target("CustomTarget")]
public sealed class CustomTarget : AsyncTaskTarget
{
protected override async Task WriteAsyncTask(LogEventInfo logEvent,CancellationToken token)
{
var layout = new NLog.Layouts.SimpleLayout("${aspnet-traceidentifier}");
string logMessage = this.Layout.Render(logEvent);
string identifier = layout.Render(logEvent);
// identifier is empty here...
identifier = RenderLogEvent("${aspnet-traceidentifier}",logEvent);
// identifier is empty here...
}
}
解决方法
也许是这样的:
[Target("CustomTarget")]
public sealed class CustomTarget : AsyncTaskTarget
{
public CustomTarget()
{
this.CorrelationId = "${aspnet-traceidentifier}";
}
public Layout CorrelationId { get; set; }
protected override Task WriteAsyncTask(LogEventInfo logEvent,CancellationToken token)
{
string logMessage = this.RenderLogEvent(this.Layout,logEvent);
string correlationId = this.RenderLogEvent(this.CorrelationId,logEvent);
// TODO - write message
}
}
另请参阅编写自定义 NLog 目标的教程:https://github.com/NLog/NLog/wiki/How-to-write-a-custom-async-target
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。