NLog日志记录学习

来源:互联网 发布:php和java哪个好学 编辑:程序博客网 时间:2024/05/29 01:54

配置NLog

  NLog支持 .Net 4.5 以及以上版本!        首先去下载NLog的DLL下载地址:http://nlog-project.org/download/  然后把下载下来的Nlog.dll ,Nlog,extension.dll 加入项目reference.之后就是配置NLog.config  格式如下: 通过在启动的时候对一些常用目录的扫描,NLog会尝试使用找到的配置信息进行自动的自我配置。 在ASP.NET项目中搜索的目录包括:标准的web程序配置文件web.config和web.config在同一目录下的web.nlog文件程序目录下的NLog.config文件NLog.dll所在目录下的NLog.dll.nlog文件 (在Nlog没有导入GAC情况下)如果定义了NLOG_GLOBAL_CONFIG_FILE环境变量,则该变量所指向的文件

子元素的配置:

  根节点是<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 />有以下属性:name - 日志源/记录者的名字 (允许使用通配符*)minlevel - 该规则所匹配日志范围的最低级别maxlevel - 该规则所匹配日志范围的最高级别level - 该规则所匹配的单一日志级别levels - 该规则所匹配的一系列日志级别,由逗号分隔。writeTo - 规则匹配时日志应该被写入的一系列目标,由逗号分隔。final - 标记当前规则为最后一个规则。其后的规则即时匹配也不会被运行。   如:<logger name="Name.Space.Class1" minlevel="Debug" writeTo="f1" /> - 名字空间Name.Space下的Class1这个类的所有级别等于或者高于Debug的日志信息都写入到“f1”这个目标里。<logger name="Name.Space.Class1" levels="Debug,Error" writeTo="f1" /> -名字空间Name.Space下的Class1这个类的所有级别等于Debug或Error的日志信息都写入到“f1”这个目标里。<logger name="Name.Space.*" writeTo="f3,f4" /> -名字空间Name.Space下所有类的所有级别的日志信息都写入到“f3”和“f4”这两个目标里。<logger name="Name.Space.*" minlevel="Debug" maxlevel="Error" final="true" /> - 名字空间Name.Space下所有类的、级别在Debug和Error之间的(包括Debug,Info,Warn,Error) 日志信息都不会被记录(因为这条规则没有定义writeTo),同时其它后续规则也都会被忽略(因为这里设置了final="true")。

配置NLog到App.config文件

 <!--这里的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="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>      <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>

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

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace NetHelper{    /// <summary>    /// 日志记录工具    /// </summary>    public class NlogTools    {        /// <summary>        /// 日志记录对象        /// </summary>        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();        /// <summary>        /// 记录Debug级别日志        /// </summary>        /// <param name="message">日志信息</param>        public static void LogDebug(string message)        {            logger.Debug(message);        }        /// <summary>        /// 错误信息日志记录        /// </summary>        /// <param name="message">错误信息</param>        /// <param name="ex">异常对象</param>        public static void LogError(string message, Exception ex)        {            StringBuilder errMsg = new StringBuilder();            errMsg.AppendLine("\r\n--------------------------------------------------------------------------------------------------------");            errMsg.AppendLine(message);            errMsg.AppendLine("当前异常InnerException:\t" + ex.InnerException);            errMsg.AppendLine("\r\n堆栈信息:\t" + ex.StackTrace);            errMsg.AppendLine("\r\n当前异常:\t" + ex.ToString());            errMsg.AppendLine("--------------------------------------------------------------------------------------------------------");            logger.Error(errMsg.ToString());        }        /// <summary>        /// 描述信息 Log        /// </summary>        /// <param name="message">描述信息</param>        public static void LogTrace(string message)        {            logger.Trace(message);        }        /// <summary>        /// 警告信息 Log        /// </summary>        /// <param name="message">警告信息</param>        public static void LogWarn(string message)        {            logger.Warn(message);        }    }}
0 0