我正在创建一个自定义目标,但我看不到从 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...
}
}