关于 NLog 输出 json 格式日志的配置

momo314相同方式共享非商业用途署名转载



nlog 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    autoReload="true"
    internalLogLevel="Trace" >

  <targets async="true">
    <target name="blackhole" xsi:type="Null" />
    <target name="console" xsi:type ="Console" />
    <target name="file" xsi:type="File" fileName="/niplogs/${var:app}.json" 
        concurrentWrites="true"
        archiveFileName="/niplogs/${var:app}.json.{#}"
        archiveAboveSize="104857600"
        archiveEvery="Day"
        archiveNumbering="Rolling"
        maxArchiveFiles="5"
        enableFileDelete="true"
        >
      <layout xsi:type="JsonLayout">
        <attribute name="app" encode="false" >
          <layout xsi:type="JsonLayout">
            <attribute name="name" layout="${var:app}"/>
            <attribute name="group" layout="${var:group}"/>
          </layout>
        </attribute>
        <attribute name="env" layout="${var:env}"/>
        <attribute name="level" layout="${level}"/>
        <attribute name="thread" layout="${threadid}"/>
        <attribute name="logger" layout="${logger}" escapeUnicode="false"/>
        <attribute name="message" layout="|${level}|${logger}|${message}" escapeUnicode="false"/>
        <attribute name="exception" encode="false" >
          <layout xsi:type="JsonLayout">
            <attribute name="type" layout="${exception:format=Type}"/>
            <attribute name="message" layout="${exception:format=Message}" escapeUnicode="false"/>
            <attribute name="stacktrace" layout="${exception:format=ToString}" escapeUnicode="false"/>
          </layout>
        </attribute>
        <attribute name="properties" encode="false" >
          <layout xsi:type="JsonLayout" includeAllProperties="true" maxRecursionLimit="2" />
        </attribute>
        <attribute name="time" layout="${longdate}"/>
        <attribute name="log" encode="false" >
          <layout xsi:type="JsonLayout">
            <attribute name="time" layout="${date:format=yyyy-MM-ddTHH\:mm\:ss.fffZ}"/>
          </layout>
        </attribute>
      </layout>
    </target>
  </targets>

  <rules>
    <!-- Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true"/>
    <!-- set up a rule to log to the azure storage target! -->
    <logger name="*" minlevel="Trace" maxlevel="Fatal" writeTo="console" />
    <logger name="*" writeTo="file" />
  </rules>
</nlog>

关于配置文件中的自定义变量的配置


public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceScopeFactory factory)
{
    LogManager.Configuration.Variables["hostname"] = Dns.GetHostName();
    // 方便按服务检索日志
    LogManager.Configuration.Variables["app"] = "app name";
    // 方便按一组服务的方式检索完成的数据流转日志(一条数据可能会在多个服务中流转)
    LogManager.Configuration.Variables["group"] = "app group name";
    LogManager.Configuration.Variables["env"] = env.EnvironmentName;
}
✎﹏ 本文来自于 momo314的神奇海螺 ,文章原创,转载请注明作者并保留原文链接。