在C#中winform程序中應用nlog日志工具,配置文件簡單應用.
文件名 nlog.config,請注意修改屬性為"始終復制",發布時候容易遇到不存在文件的錯誤提示.
通過NuGet添加對應framework版本的nlog工具,如果framework切換版本,需要卸載之后,重新安裝.
簡單配置的nlog.config文件.
項目實際在用:會在bin文件夾下創建logs文件夾,然后生成對應的log文件,可以用記事本打開查看日志內容.
內容如下:
樣式1:
<?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 async="true" maxarchivefiles="2">
<target name="log_file" xsi:type="File" fileName="${basedir}/logs/${date:format=yyyyMMdd}.log"
layout="[============================${newline}${date} ${appdomain} ${logger} ${level}${newline}${message}${newline}${exception}]"/>
</targets>
<rules>
<logger name="*" minlevel="trace" writeTo="log_file"></logger>
</rules>
</nlog>
樣式2:
<?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 async="true" maxarchivefiles="2">
<target name="log_file" xsi:type="File" fileName="${basedir}/logs/${date:format=yyyyMMdd}.log"
layout="[==${date} ${appdomain} ${logger} ${level}==]${newline}${message}${newline}${exception}${newline}"/>
</targets>
<rules>
<logger name="*" minlevel="trace" writeTo="log_file"></logger>
</rules>
</nlog>
