·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> Nlog

Nlog

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

安装Nlog

通过NuGet的Console方式

PM> Install-Package NLog

或者直接用Manage

添加配置文件内容

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <configSections>
 4     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
 5   </configSections>
 6   <nlog xmlns="http://www.nlog-PRoject.org/schemas/NLog.xsd"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 7     <targets>
 8       <target name="file" xsi:type="File" fileName="${shortdate}.txt"
 9               layout="${date:format=HH\:mm\:ss}-${stacktrace}-${message}"/>
10     </targets>
11     <rules>
12       <logger name="*" minlevel="Error" writeTo="file"/>
13     </rules>
14   </nlog>
15 </configuration>

调用

1 string message = "这是一个异常消息";
2 NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
3 logger.Error(message);

相关介绍链接

http://nlog-project.org/

https://github.com/nlog/nlog/wiki/Configuration-file

http://www.cnblogs.com/dflying/archive/2006/12/06/584426.html(翻译版)

 常用配置(异常和信息记录分类按日期保存):

<configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="fileError" xsi:type="File" fileName="logs/Error/${shortdate}.txt" layout="${date:format=HH\:mm\:ss}-${stacktrace}-${message}"/>
      <target name="fileInfo" xsi:type="File" fileName="logs/Info/${shortdate}.txt" layout="${date:format=HH\:mm\:ss}-${message}"/>
    </targets>
    <rules>
      <logger name="*" levels="Error" writeTo="fileError"/>
      <logger name="*" levels="Info" writeTo="fileInfo"/>
    </rules>
  </nlog>