Nlog.config 分享

<?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"
      throwExceptions="false"
      internalLogLevel="Off" >
  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <!--<extensions>
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>-->
  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->
    <!-- json output for log level: Fatal and Error-->
    <target xsi:type="File" name="jsonFileError" keepFileOpen="true"  OpenFileCacheTimeout="60"
            archiveAboveSize="5242880" maxArchiveFiles="20" maxArchiveDays="14"
                   AutoFlush="false" openFileFlushTimeout="5"  ConcurrentWrites="false"
            fileName="${basedir}/logs/error/${shortdate}.${processinfo:property=StartTime:format=yyyyMMddHHmmss}.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="threadId" layout="${threadid}"/>
        <attribute name="callsite" layout="${callsite:includSourcePath=true}"/>
        <attribute name="message" layout="${message}"  escapeUnicode="false"/>
        <attribute name="exception" layout="${exception:format=ToString}" escapeUnicode="false"/>
        <attribute name="stackTrace" layout="${StackTrace:topFrames=10}" />
      </layout>
    </target>

    <!-- json output for log level: Warn and Info-->
    <target xsi:type="File" name="jsonFileInfo" keepFileOpen="true" OpenFileCacheTimeout="60"
            archiveAboveSize="5242880" maxArchiveFiles="20" maxArchiveDays="7"
                    AutoFlush="false" openFileFlushTimeout="5"  ConcurrentWrites="false"
            fileName="${basedir}/logs/info/${shortdate}.${processinfo:property=StartTime:format=yyyyMMddHHmmss}.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="threadId" layout="${threadid}"/>
        <attribute name="callsite" layout="${callsite:includSourcePath=true}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="exception" layout="${exception:format=ToString}" />
        <attribute name="stackTrace" layout="${StackTrace:topFrames=10}" />
      </layout>
    </target>

    <!-- json output for log level: Trace and Debug-->
    <target xsi:type="File" name="jsonFileTrace" keepFileOpen="true" OpenFileCacheTimeout="60"
            archiveAboveSize="5242880" maxArchiveFiles="20" maxArchiveDays="7"
                    AutoFlush="false" openFileFlushTimeout="5"  ConcurrentWrites="false"
            fileName="${basedir}/logs/trace/${shortdate}.${processinfo:property=StartTime:format=yyyyMMddHHmmss}.log.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="threadId" layout="${threadid}"/>
        <attribute name="callsite" layout="${callsite:includSourcePath=true}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="exception" layout="${exception:format=ToString}" />
        <attribute name="stackTrace" layout="${StackTrace:topFrames=10}" />
      </layout>
    </target>

    <!-- json output for log level: Trace and Debug-->
    <target xsi:type="File" name="myJsonFileTrace" keepFileOpen="true" OpenFileCacheTimeout="60"
            archiveAboveSize="5242880" maxArchiveFiles="20" maxArchiveDays="7" 
                    AutoFlush="false" openFileFlushTimeout="5"  ConcurrentWrites="false"
            fileName="${basedir}/logs/my/${shortdate}.${processinfo:property=StartTime:format=yyyyMMddHHmmss}.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="threadId" layout="${threadid}"/>
        <attribute name="message" layout="${message}"  escapeUnicode="false"/>
        <attribute name="exception" layout="${exception:format=ToString}" escapeUnicode="false"/>
        <attribute name="callsite" layout="${callsite:includSourcePath=true}"/>
      </layout>
    </target>

    <target xsi:type="File" name="myAdminViewJsonFileTrace" keepFileOpen="true" OpenFileCacheTimeout="60"
            archiveAboveSize="5242880" maxArchiveFiles="20" maxArchiveDays="7"
                    AutoFlush="false" openFileFlushTimeout="5"  ConcurrentWrites="false"
            fileName="${basedir}/logs/my/adminView/${shortdate}.${processinfo:property=StartTime:format=yyyyMMddHHmmss}.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="threadId" layout="${threadid}"/>
        <attribute name="callsite" layout="${callsite:includSourcePath=true}"/>
        <attribute name="message" layout="${message}"  escapeUnicode="false"/>
        <attribute name="exception" layout="${exception:format=ToString}" escapeUnicode="false"/>
      </layout>
    </target>

    <target xsi:type="File" name="myControllerHeartbeatJsonFileTrace" keepFileOpen="true" OpenFileCacheTimeout="60"
            archiveAboveSize="5242880" maxArchiveFiles="20" maxArchiveDays="7"
                    AutoFlush="false" openFileFlushTimeout="5"  ConcurrentWrites="false"
            fileName="${basedir}/logs/my/controllerHeart/${shortdate}.${processinfo:property=StartTime:format=yyyyMMddHHmmss}.log">
      <layout xsi:type="JsonLayout" includeAllProperties="true">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="threadId" layout="${threadid}"/>
        <attribute name="callsite" layout="${callsite:includSourcePath=true}"/>
        <attribute name="message" layout="${message}"  escapeUnicode="false"/>
        <attribute name="exception" layout="${exception:format=ToString}" escapeUnicode="false"/>
      </layout>
    </target>
    
    <!--txt输出配置-->

    <target name="log_fileerror" xsi:type="File" keepFileOpen="true" OpenFileCacheTimeout="60" AutoFlush="false" openFileFlushTimeout="5" ConcurrentWrites="false"
          
            fileName="${basedir}/logs/error/${shortdate}.log" layout="${longdate} | ${uppercase:${level}}| threadId:${threadid} | ${callsite:includSourcePath=true} | ${message} | ${exception:format=ToString} | ${StackTrace:topFrames=10}" />

    <target name="log_fileinfo" xsi:type="File" keepFileOpen="true"  OpenFileCacheTimeout="60" AutoFlush="false" openFileFlushTimeout="5" ConcurrentWrites="false"
            archiveAboveSize="5000000" maxArchiveFiles="20" archiveNumbering="DateAndSequence"
            fileName="${basedir}/logs/info/${shortdate}.log" layout="${longdate} | ${uppercase:${level}}| threadId:${threadid} | ${callsite:includSourcePath=true} | ${message}" />

    <!--控制台输出-->
    <target name="colorConsole" xsi:type="Console"  layout="${longdate} | ${uppercase:${level}}| threadId:${threadid} | ${callsite:includSourcePath=true} | ${message} | ${exception:format=ToString} | ${StackTrace:topFrames=10}"/>
    <!--
    Write events to a file with the date in the filename.
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
  </targets>
  <rules>
    <!-- add your logging rules here-->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
   
    <logger name="*" minlevel="Trace"  writeTo="colorConsole"/>  -->
    <logger name="*" minlevel="Trace" maxlevel="Fatal"  writeTo="jsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.AlarmHandler" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.AudioTransferHandler" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.DCRSHostService" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.DefaultServiceHolder" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.MCOSHostedService" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.MessageConsumingHostedService" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.MessageGuardHostedService" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.MessageStatusHandler" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.TTSHandler" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.WebSocketControllerListener" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Impl.*" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.Services.*" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.Shared.Business.*" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <!--<logger name="" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>
    <logger name="" minlevel="Trace" maxlevel="Fatal" writeTo="myJsonFileTrace"/>-->
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.AcosAdminPublishRtusHostService" minlevel="Trace" maxlevel="Fatal" writeTo="myAdminViewJsonFileTrace"/>
    <logger name="Stee.FGMS.ACOS.MainService.GrpcServer.MessageProcessing.Background.ControllerHeartbeatHostedService" minlevel="Trace" maxlevel="Fatal" writeTo="myControllerHeartbeatJsonFileTrace"/>
    <logger name="*" minlevel="Info" maxlevel="Warn" final="true" writeTo="jsonFileInfo"/>
    <logger name="*" minlevel="Error" maxlevel="Fatal" final="true" writeTo="jsonFileError"/>

    <!--<logger name="*" writeTo="log_filetrace" />-->
  </rules>
</nlog>

几点说明:

1.这个日志是以 日期+程序启动时间.log命令的

2.通过escapeUnicode="false"避免内容出现乱码

3.maxArchiveFiles="20" maxArchiveDays="7" 配置来定时删除旧的日志(避免出现archiveNumbering="DateAndSequence",会使删除过期的日志配置失效)

4. final="true"来 区别 匹配了该配置后就不再匹配下面的了

posted @ 2022-09-26 15:24  N伽莫夫J  阅读(230)  评论(0编辑  收藏  举报