日志框架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>
复制代码

 

posted @   代码沉思者  阅读(1234)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示