日志功能本来是一件非常好的事情,能帮助我们分析和解决很多问题。但是,如果让日志影响到性能,这就需要权衡一下了。没关系,本篇博客咱们不仅来解决这个问题,咱们更要眼见为实,带图带真相让你用的明明白白。
日志实现异步功能
其实呢就一句话。。
配置文件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>