在C#中winform程序中应用nlog日志工具

在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>

  

原文地址:https://www.cnblogs.com/iframe/p/7609380.html