NLog日志工具的使用及配置-输出年/月文件夹

来源:互联网 发布:月中天 知乎 编辑:程序博客网 时间:2024/06/15 15:07

NLog日志工具的使用及配置-输出年/月文件夹


1,(打开【工具】》【库程序包管理器】》【程序包管理器控制台】) 在程序包控制台输入命令install-package nlog,会自动引用nlog并下载资源文件。

nlog


2,在程序包控制台输入命令install-package nlog.config,会添加nlog配置文件

nlog.config

完成添加


3,配置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"      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"      autoReload="true"      throwExceptions="false"      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"><targets>    <!--VS输出窗口-->    <target name="debugger" xsi:type="Debugger"                    layout="${date:format=HH\:mm\:ss} | ${level:padding=-5} | ${message}" />    <!--保存至文件-->    <target name="error_file" xsi:type="File"  encoding="utf-8"            fileName="${basedir}/Logs/${date:format=yyyy}/${date:format=MM}/${shortdate}.log"                    layout="${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />  </targets>  <rules>    <logger name="*" minlevel="Debug" writeTo="debugger" />    <logger name="*" minlevel="Error" writeTo="error_file" />  </rules></nlog>

4,最后在程序中添加代码

public static Logger logger = LogManager.GetCurrentClassLogger();

这里写图片描述


5,运行结果

输出
这里写图片描述

文件
这里写图片描述

6,其他资料

多种不同配置方式及详细描述

github地址

                                                                                 by LRF 20170630 
原创粉丝点击