配置 NLog 的布局以显示所有事件上下文条目

configuring NLog's layout to show all the event-context entries

我目前正在开发 webapi2 应用程序,并且正处于使用 NLog 管理我的日志的阶段。

在我的应用程序中,我以这种方式使用 LogEventInfo.Properties 字典以键值对方式登录:

thisController.LogParams.Add("ControllerName",controllerName);
thisController.LogParams.Add("ActionName", actionName);
thisController.LogParams.Add("TotalTime", actionWatch.ElapsedMilliseconds);
LogEventInfo logEvent = new LogEventInfo() 
       { 
           Message = string.IsNullOrEmpty(thisController.LogMessage)? "Finished Successfuly":thisController.LogMessage,
           Level = LogLevel.Info,
           TimeStamp = DateTime.Now
       };
logEvent.Properties.Merge(thisController.LogParams);
logger.Log(logEvent);

一切正常,但我似乎无法找到呈现布局的方式,因此它打印了 LogEventInfo.Properties 字典中的所有键值条目。

假设我的目标是一个文件,那么我必须明确提及键名, 有没有办法让它显示字典的所有内容? 这就是我今天的做法,我只能记录我知道的条目:

<target name="f1" 
  xsi:type="File" 
  fileName="${basedir}\logs\log.txt" 
  maxArchiveFiles="60" 
  archiveNumbering="Date" 
  archiveDateFormat="yyyyMMdd" 
  archiveEvery="Day" 
  layout="${longdate} : ${callsite:className=true:methodName=true} : ${event-context:item=ControllerName} : ${event-context:item=ActionName} : ${event-context:item=TotalTime} : ${message}" />

没有可以一次呈现所有属性的内置支持。如果您查看 event-context layout renderer 中的代码,那么它只会根据项目添加 属性 的单个值,即 属性 名称。

您可以创建自定义布局呈现器,它可以打印 LogEventInfo 中的所有属性。不是基于项目获取 属性 的值,而是将它们全部添加到字符串生成器,它将被打印到目标。

using System;
using System.Globalization;
using System.Text;
using NLog.Config;
namespace CustomLayoutRenderer
{
    /// <summary>
    /// Log event context data.
    /// </summary>
    [LayoutRenderer("event-context-all")]
    public class EventContextLayoutRenderer : LayoutRenderer
    {
        /// <summary>
        /// Renders the specified log event context item and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            foreach (var item in logEvent.Properties)
            {
                // item is a keyvalue pair object you can custom format the key value as needed.
                builder.Append(Convert.ToString(item.Value, CultureInfo.InvariantCulture));
            }

        }
    }
}

您可以在 Nlog 配置文件中使用 <extension> 标记注册此自定义布局渲染器。

<nlog> 
  <extensions> 
    <!-- Add the assembly name which contains the custom layout renderer -->
    <add assembly="MyAssembly"/> 
  </extensions> 
  <targets> 
    <target name="f1" type="file"  layout="${longdate} ${event-context-all}" 
            fileName="${basedir}/logs/logfile.log" />
  </targets> 
</nlog>

这将打印所有属性 values only 而无需项目名称。

NLog 中有一个内置的布局渲染器可用于此目的 - ${all-event-properties}。默认情况下,它以键值样式发出所有事件上下文属性,以逗号分隔。

查看文档了解更多详情 https://github.com/NLog/NLog/wiki/All-Event-Properties-Layout-Renderer