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

Ben*_*uri 8 c# nlog

我目前正在开发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);
Run Code Online (Sandbox Code Playgroud)

一切正常,但我似乎无法找到渲染布局的方式,因此它打印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}" />
Run Code Online (Sandbox Code Playgroud)

pkm*_*iec 5

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

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