NLog自定义Layout Renderer

2020-04-15 16:21:52 浏览数 (1)

本文自定义一个NLog Layout Renderer(显示HttpClient请求的耗时)

什么是Layout Renderer?

nlog日志上输出的特定字段,便于检索和分类。

代码语言:javascript复制
# 截取自nlog.config配置文件
<target name="bce-request"
代码语言:javascript复制
        xsi:type="File"
  layout="${date:format=yy/MM/dd HH:mm:ss} [${level}].[${logger}].[${threadid}}].[${elapse}]${newline}${message} ${exception:format=tostring}"
       fileName="${logDir}/bce-request.log" encoding="utf-8"/>

以上配置输出如下日志:

代码语言:javascript复制
19/12/08 22:46:29 [Info].[System.Net.Http.HttpClient.bce-request.LogicalHandler].[6}].[415.2504]HTTP request http://localhost:5000/v1/eqid/e741e8d600151edc000000035decf3bf after 415.2504ms end -OK 19/12/08 22:47:15 [Info].[System.Net.Http.HttpClient.bce-request.LogicalHandler].[40}].[80.2951]HTTP request http://localhost:5000/v1/eqid/2a41e8d600151edc000000028decf3bf after 80.2951ms end -OK 19/12/08 22:48:06 [Info].[System.Net.Http.HttpClient.bce-request.LogicalHandler].[43}].[36.8624]HTTP request http://localhost:5000/v1/eqid/1a41e8d600151edc000000028decf3bf after 36.8624ms end -OK

头脑风暴

nlog所有的日志Render依赖日志写入时的信息, 因此我们在写入日志时附带该Renderer值, 然后配置nlog显示日志时提取该Renderer值。

1

写入日志时,为Message传入参数{Url}, {Elapse}, {StatusCode}, 这三个参数值可被提取作为 Renderer

代码语言:javascript复制
public class CustomHttpMessageHandler : DelegatingHandler    {        private readonly ILogger _logger;
        public AttachTraceIdScopeHttpMessageHandler(ILogger logger)        {            _logger = logger ?? throw new ArgumentNullException(nameof(logger));        }
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,            CancellationToken cancellationToken)        {            if (request == null)            {                throw new ArgumentNullException(nameof(request));            }
            var stopwatch = Stopwatch.StartNew();            var response = await base.SendAsync(request, cancellationToken);            stopwatch.Stop();            _logger.Log(LogLevel.Information, new EventId(101, "Request End"),                "HTTP request {Url} after {Elapse}ms end -{StatusCode}",                request.RequestUri, stopwatch.Elapsed.TotalMilliseconds, response.StatusCode);            return response;        }    }

2

添加自定义LayOutRenderer

  ① 简单的lambda方式,

② 我们采用稍灵活的自定义类方式:

关键点是实现LayoutRenderer的抽象方法Append, 从LogEventInfo中提出Renderer值:

代码语言:javascript复制
[LayoutRenderer("elapse")]    public class ElapseLayoutRenderer : LayoutRenderer    {        protected override void Append(StringBuilder builder, LogEventInfo logEvent)        {            builder.Append(logEvent.Properties["Elapse"].ToString());        }    }# 参数Url、Elapse、StatusCode均可在LogEventInfo.Prpperties键值对提取

3

按照文档的要求,尽早注册自定义Nlog Layout Renderer:

代码语言:javascript复制
public static void Main(string[] args){      LayoutRenderer.Register<ElapseLayoutRenderer>("elapse");   ......}

END

0 人点赞