NLog 使用说明

  最近公司为上新项目做前期的准备工作,需要用到日志记录的功能。现在功能相对强大的日志跟踪记录的组件有两款,Log4Net 和 NLog。

NLog的配置比Log4Net的简单很多,功能上大同小异。这里给没有接触过NLog的朋友做下整理与使用方法的介绍。

     【NLog相关文章链接】

   以下文章链接自Dflying Chen 的Blog

      1、NLog文章系列——系列文章目录以及简要介绍

    2、NLog文章系列——入门教程(上)

      3、NLog文章系列——入门教程(中)

      4、NLog文章系列——入门教程(下)

      5、NLog文章系列——与Visual Studio集成

      6、NLog文章系列——如何配置NLog

      7、NLog文章系列——如何排错

      8、NLog文章系列——如何写自定义布局生成器(Layout Renderer)

      9、NLog文章系列——如何编写自定义的过滤器(Filter)

     10、NLog文章系列——如何优化日志性能

    【NLog 使用】

  1、WinForm下配置文件

    添加NLog.dll的引用

           在NLog.dll的文件夹下创建NLog.dll.nlog,写如下配置信息

<?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" >

<targets>
    <target name="file" xsi:type="File" fileName="${basedir}/log.txt" 
            layout="[${date:format=yyyy-MM-dd HH\:mm\:ss}][${level}] ${message} ${exception}"/>
</targets>
<rules>
    <logger name="*" minlevel="debug" writeTo="file"></logger>
</rules> </nlog>

 

      2、Asp.net下配置文件

     添加NLog.dll的引用

           在根目录下创建NLog.config,写和WinForm下一样的配置信息

    或者把配置信息写到Web.config 的 configuration 节点中,再加上下面这句

      <configSections>
            <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
      </configSections>

      3、调用方法

    创建实例:NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

           调用相应的方法:log.Debug("日志输出测试");

 

  注:

    详细的介绍和一些方法,参数,环境变量的使用,请下载NLog的示例及使用帮助。

           下载地址:http://download.csdn.net/detail/andy_sue/4217457

           有疑问请发Email : andysue@live.cn

 

  

posted @ 2012-04-11 16:51  苏健  阅读(1922)  评论(0)    收藏  举报