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环境变量,则该变量所指向的文件
<?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" >


  <!-- optional, add some variabeles
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!-- 
  See https://github.com/nlog/nlog/wiki/Configuration-file 
  for information on customizing logging rules and outputs.
   -->
  <targets>

    <!-- 
    add your targets here 
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->

    <!--
    Write events to a file with the date in the filename. 
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
    <target xsi:type="File" name="dbg" fileName="${basedir}/logs/Debug${shortdate}.log"
         layout="${longdate} ${uppercase:${level}} ${message}" />

    <target xsi:type="File" name="Eor" fileName="${basedir}/logs/Error${shortdate}.log"
         layout="${longdate} ${uppercase:${level}} ${message}" />
    <target xsi:type="File" name="Tre" fileName="${basedir}/logs/Trace${shortdate}.log"
         layout="${longdate} ${uppercase:${level}} ${message}" />
    <target xsi:type="File" name="War" fileName="${basedir}/logs/Warn${shortdate}.log"
         layout="${longdate} ${uppercase:${level}} ${message}" />
  </targets>
  

  <rules>
    <!-- add your logging rules here -->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->
    <logger name="*" minlevel="Debug" writeTo="dbg" />
    <logger name="*" minlevel="Error" writeTo="Eor" />
    <logger name="*" minlevel="Trace" writeTo="Tre" />
    <logger name="*" minlevel="Warn" writeTo="War" />
  </rules>
</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文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--这里的ConfigSections 必须要处于根节点下的第一个节点,至于原因目前还不清楚-->
  <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">
    <!--  See http://nlog-project.org/wiki/Configuration_file  for information on customizing logging rules and outputs.   -->
    <targets>
      <target xsi:type="File" name="f" fileName="${basedir}/APP_Data/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="f" />
    </rules>
  </nlog>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

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

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






附件列表

     

    posted @   镹丶天  阅读(845)  评论(0编辑  收藏  举报
    编辑推荐:
    · SQL Server如何跟踪自动统计信息更新?
    · AI与.NET技术实操系列:使用Catalyst进行自然语言处理
    · 分享一个我遇到过的“量子力学”级别的BUG。
    · Linux系列:如何调试 malloc 的底层源码
    · AI与.NET技术实操系列:基于图像分类模型对图像进行分类
    阅读排行:
    · 对象命名为何需要避免'-er'和'-or'后缀
    · JDK 24 发布,新特性解读!
    · Java24你发任你发,我用Java8
    · C# 中比较实用的关键字,基础高频面试题!
    · .NET Core奇技淫巧之WinForm使用Python.NET并打包

    Throw new Exception("object not found");

    点击右上角即可分享
    微信分享提示