NLog日志记录

配置NLog

        NLog支持 .Net 4.5 以及以上版本!
    
        首先去下载NLog的DLL下载地址:http://nlog-project.org/download/  然后把下载下来的Nlog.dll ,Nlog,extension.dll 加入项目reference.

之后就是配置NLog.config  格式如下:
 通过在启动的时候对一些常用目录的扫描,NLog会尝试使用找到的配置信息进行自动的自我配置。

 在ASP.NET项目中搜索的目录包括:

  1. 标准的web程序配置文件web.config
  2. web.config在同一目录下的web.nlog文件
  3. 程序目录下的NLog.config文件
  4. NLog.dll所在目录下的NLog.dll.nlog文件 (在Nlog没有导入GAC情况下)
  5. 如果定义了NLOG_GLOBAL_CONFIG_FILE环境变量,则该变量所指向的文件
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  5. autoReload="true"
  6. throwExceptions="false"
  7. internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >
  8. <!-- optional, add some variabeles
  9. https://github.com/nlog/NLog/wiki/Configuration-file#variables
  10. -->
  11. <variable name="myvar" value="myvalue"/>
  12. <!--
  13. See https://github.com/nlog/nlog/wiki/Configuration-file
  14. for information on customizing logging rules and outputs.
  15. -->
  16. <targets>
  17. <!--
  18. add your targets here
  19. See https://github.com/nlog/NLog/wiki/Targets for possible targets.
  20. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
  21. -->
  22. <!--
  23. Write events to a file with the date in the filename.
  24. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
  25. layout="${longdate} ${uppercase:${level}} ${message}" />
  26. -->
  27. <target xsi:type="File" name="dbg" fileName="${basedir}/logs/Debug${shortdate}.log"
  28. layout="${longdate} ${uppercase:${level}} ${message}" />
  29. <target xsi:type="File" name="Eor" fileName="${basedir}/logs/Error${shortdate}.log"
  30. layout="${longdate} ${uppercase:${level}} ${message}" />
  31. <target xsi:type="File" name="Tre" fileName="${basedir}/logs/Trace${shortdate}.log"
  32. layout="${longdate} ${uppercase:${level}} ${message}" />
  33. <target xsi:type="File" name="War" fileName="${basedir}/logs/Warn${shortdate}.log"
  34. layout="${longdate} ${uppercase:${level}} ${message}" />
  35. </targets>
  36. <rules>
  37. <!-- add your logging rules here -->
  38. <!--
  39. Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
  40. <logger name="*" minlevel="Debug" writeTo="f" />
  41. -->
  42. <logger name="*" minlevel="Debug" writeTo="dbg" />
  43. <logger name="*" minlevel="Error" writeTo="Eor" />
  44. <logger name="*" minlevel="Trace" writeTo="Tre" />
  45. <logger name="*" minlevel="Warn" writeTo="War" />
  46. </rules>
  47. </nlog>

子元素的配置

        根节点是<nlong></nlong>,两个命名空间是可选的,但是为了智能感应,你还是把它写上吧。

  • <targets /> – defines log targets/outputs ,声明目标
  • <rules /> – defines log routing rules ,声明规则
  • <extensions /> – loads NLog extensions from the *.dll file  加载dll扩展(其实我不懂,没用过)
  • <include /> – includes external configuration file   包含外部配置文件
  • <variable /> – sets the value of a configuration variable 为配置变量赋值

路由规则

    <rules />区域定义了日志的路由规则。每一个路由表项就是一个<logger />元素。<logger />有以下属性:
  1. name - 日志源/记录者的名字 (允许使用通配符*)
  2. minlevel - 该规则所匹配日志范围的最低级别
  3. maxlevel - 该规则所匹配日志范围的最高级别
  4. level - 该规则所匹配的单一日志级别
  5. levels - 该规则所匹配的一系列日志级别,由逗号分隔。
  6. writeTo - 规则匹配时日志应该被写入的一系列目标,由逗号分隔。
  7. final - 标记当前规则为最后一个规则。其后的规则即时匹配也不会被运行。

   如:

  1. <logger name="Name.Space.Class1" minlevel="Debug" writeTo="f1" /> - 名字空间Name.Space下的Class1这个类的所有级别等于或者高于Debug的日志信息都写入到“f1”这个目标里。
  2. <logger name="Name.Space.Class1" levels="Debug,Error" writeTo="f1" /> -名字空间Name.Space下的Class1这个类的所有级别等于Debug或Error的日志信息都写入到“f1”这个目标里。
  3. <logger name="Name.Space.*" writeTo="f3,f4" /> -名字空间Name.Space下所有类的所有级别的日志信息都写入到“f3”和“f4”这两个目标里。
  4. <logger name="Name.Space.*" minlevel="Debug" maxlevel="Error" final="true" /> - 名字空间Name.Space下所有类的、级别在Debug和Error之间的(包括Debug,Info,Warn,Error) 日志信息都不会被记录(因为这条规则没有定义writeTo),同时其它后续规则也都会被忽略(因为这里设置了final="true")。

配置NLog到App.config文件

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <!--这里的ConfigSections 必须要处于根节点下的第一个节点,至于原因目前还不清楚-->
  4. <configSections>
  5. <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  6. </configSections>
  7. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  8. <!-- See http://nlog-project.org/wiki/Configuration_file for information on customizing logging rules and outputs. -->
  9. <targets>
  10. <target xsi:type="File" name="f" fileName="${basedir}/APP_Data/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" />
  11. </targets>
  12. <rules>
  13. <logger name="*" minlevel="Trace" writeTo="f" />
  14. </rules>
  15. </nlog>
  16. <startup>
  17. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  18. </startup>
  19. </configuration>

在代码中使用Log记录日志信息

  1.      private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  2. static void Main(string[] args)
  3. {
  4. //Logger logger = LogManager.GetLogger("MyClassName");
  5. logger.Debug("Test Logger Message");
  6. }






附件列表

     

    posted @ 2016-06-24 11:51  镹丶天  阅读(838)  评论(0编辑  收藏  举报

    Throw new Exception("object not found");