日志框架Nlog之異步記錄


日志功能本來是一件非常好的事情,能幫助我們分析和解決很多問題。但是,如果讓日志影響到性能,這就需要權衡一下了。沒關系,本篇博客咱們不僅來解決這個問題,咱們更要眼見為實,帶圖帶真相讓你用的明明白白。

日志實現異步功能

           其實呢就一句話。。

           配置文件targets中配置async="true"即為異步。默認或寫false都為同步。

例如:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">

  <targets async="true">

    <target name="jsonFile" xsi:type="File" fileName="${basedir}/logs/${level}/${date:format=yyyy-MM-dd}.json" archiveFileName="${basedir}/logs/${level}/log.{#}.json" archiveNumbering="DateAndSequence" archiveAboveSize="10483760" archiveDateFormat="yyyyMMdd" encoding="utf-8">
      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}"/>
        <attribute name="machine" layout="${machinename}"/>
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}"/>
        <!--<attribute name="cost" layout="${mdc:item=cost}"/>
        <attribute name="nested">
          <layout type="JsonLayout">
            <attribute name="message" layout="${message}"/>
            <attribute name="exception" layout="${exception}"/>
          </layout>
        </attribute>-->
      </layout>
    </target>

    <target name="warningFile" xsi:type="File" fileName="${basedir}/warn/log.text" archiveFileName="${basedir}/${logger}/${level}/log.{#}.json" archiveNumbering="DateAndSequence" archiveAboveSize="10483760" archiveDateFormat="yyyyMMdd">
      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}"/>
        <attribute name="machine" layout="${machinename}"/>
        <attribute name="logger" layout="${logger}"/>
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}"/>
      </layout>
    </target>

    <target name="fatalFile" xsi:type="File" fileName="${basedir}/fatal/log.text" archiveFileName="log.{#}.json" archiveNumbering="DateAndSequence" archiveAboveSize="10483760" archiveDateFormat="yyyyMMdd">
      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}"/>
        <attribute name="machine" layout="${machinename}"/>
        <attribute name="logger" layout="${logger}"/>
        <attribute name="message" layout="${message}"/>
      </layout>
    </target>

  </targets>

  <rules>
    <!--<logger name="測試" minlevel="Debug" maxlevel="Error" writeTo="nxlog_json"/>
      <logger name="bar" minlevel="Debug" maxlevel="Error" writeTo="jsonFile"/>
      <logger name="*" levels="Debug,Warn" writeTo="warningFile,jsonFile"/>-->
    <logger name="*" minlevel="Debug" maxlevel="Error" writeTo="jsonFile"/>
  </rules>

</nlog>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM