简体   繁体   English

NLog,在自定义目标中访问布局渲染器

[英]NLog, access layout renderers within custom target

I'm creating a custom target but I see no way to access layout renderers value such as aspnet-traceidentifier https://github.com/NLog/NLog/wiki/AspNetTraceIdentifier-Layout-Renderer from Write method.我正在创建一个自定义目标,但我看不到从 Write 方法访问布局渲染器值的方法,例如 aspnet-traceidentifier https://github.com/NLog/NLog/wiki/AspNetTraceIdentifier-Layout-Renderer

Here is the code I use:这是我使用的代码:

[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...
    }  
}

Maybe something like this:也许是这样的:

    [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
        } 
    } 

See also the tutorial for writing custom NLog target: https://github.com/NLog/NLog/wiki/How-to-write-a-custom-async-target另请参阅编写自定义 NLog 目标的教程: https://github.com/NLog/NLog/wiki/How-to-write-a-custom-async-target

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM