很实用的NLog配置分享

2022-10-16,,,

前言

nlog是一个基于.net平台编写的类库,我们可以使用nlog在应用程序中添加极为完善的跟踪调试代码。本文主要介绍的是关于nlog配置的相关内容,下面话不多说了,来一起看看详细的介绍吧

nlog配置

新建一个文件命名为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">

 <targets>
 <target name="asyncfile" xsi:type="asyncwrapper">
  <target name="log_file" xsi:type="file"
    filename="${basedir}/logs/${shortdate}/${shortdate}.txt"
    layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"
    archivefilename="${basedir}/archives/${shortdate}-{#####}.txt"
    archiveabovesize="102400"
    archivenumbering="sequence"
    concurrentwrites="true"
    keepfileopen="false" />
 </target>
 <target name="console" xsi:type="coloredconsole" layout="[${date:format=hh\:mm\:ss}]:${message} ${exception:format=message}" />
 </targets>

 <rules>
 <logger name="*" minlevel="error" writeto="asyncfile" />
 <logger name="*" minlevel="debug" writeto="console" />
 </rules>
</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">

 <variable name="loglayout"
   value="logger:${logger}${newline}date:${longdate} level:${uppercase:${level}}${newline}message:${message} ${newline}${onexception:exception:${exception:format=tostring}${newline}}" />

 <targets>
 <target name="asyncfile" xsi:type="asyncwrapper">
  <target name="log_file" xsi:type="file"
    filename="${basedir}/logs/${shortdate}/${shortdate}.txt"
    layout="${loglayout}"
    archivefilename="${basedir}/archives/${shortdate}-{#####}.txt"
    archiveabovesize="102400"
    archivenumbering="sequence"
    concurrentwrites="false"
    keepfileopen="true" 
    encoding="utf-8"
    openfilecachetimeout="30"/>
 </target>
 </targets>

 <rules>
 <logger name="*" minlevel="info" writeto="asyncfile" />
 </rules>
</nlog>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。

《很实用的NLog配置分享.doc》

下载本文的Word格式文档,以方便收藏与打印。