引入包如下
配置文件
<?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">
<extensions>
<add assembly="NlogViewer"/>
</extensions>
<targets >
<default-wrapper xsi:type="AsyncWrapper"></default-wrapper>
<!-- 保存至文件 -->
<target name="log_file" xsi:type="File"
fileName="${basedir}/Logs/${shortdate}/${level:uppercase=false:padding=-5}.txt"
layout="${longdate} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"
/>
<!-- 输出至Debugger -->
<target name="debugger" xsi:type="Debugger" layout="NLog: ${date:format=HH\:mm\:ss} Debugger: ${message}" />
<!-- 输出至控制台 -->
<target name="console" xsi:type="ColoredConsole" layout="${longdate} ${message} ${exception:format=tostring}"/>
<!-- 输出至NLogViewer控件 -->
<target xsi:type="NLogViewer" name="logCtrl" layout="${longdate} ${message} ${exception:format=tostring}" />
</targets>
<!-- 输出规则 -->
<rules>
<logger name="*" level="Debug" writeTo="logCtrl" />
<logger name="*" level="Info" writeTo="logCtrl" />
<logger name="*" levels="Error" writeTo="logCtrl" />
<logger name="*" level="Fatal" writeTo="logCtrl" />
<logger name="*" level="Warn" writeTo="logCtrl" />
<logger name="*" level="Info" writeTo="log_file" />
<logger name="*" levels="Error" writeTo="log_file" />
<logger name="*" level="Fatal" writeTo="log_file" />
<logger name="*" level="Warn" writeTo="log_file" />
</rules>
</nlog>
xml