NLog日志使用方法

Nlog是一款可以通过自动化配置来把log信息指定写入 win console,Sql server,甚至是通过STMP 发送邮件的log engine

首先下载Nlog DLL,通过网上直接download 或者nuget 下载DLL都可以。

http://nlog-project.org/download/

然后把下载下来的Nlog.dll ,Nlog,extension.dll 加入项目reference.


我暂时只加载了Nlog,因为还没去研究Nlog.extension有什么功能。

之后就是配置文件了,(Nlog Configuration), 它其实是一个 config文件Name:(Nlog.config

格式如下:

01 <?xml version="1.0" encoding="utf-8" ?>
03       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
04  
05   <!--
07   for information on customizing logging rules and outputs.
08    -->
09   <targets>
10     <!-- add your targets here -->
11      
12     <!--
13     <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
14             layout="${longdate} ${uppercase:${level}} ${message}" />
15     -->
16   </targets>
17  
18   <rules>
19     <!-- add your logging rules here -->
20      
21     <!--
22     <logger name="*" minlevel="Trace" writeTo="f" />
23     -->
24   </rules>
25 </nlog>

或者如果你是WebAPP的话,可以把config直接写在web.config的<configuration>节点下, 我比较喜欢这种

<configuration>
  <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>

</configuration>



接下来就是比较有技术含量的东西了,如何把log写入数据库,或者发邮件通知,也是通过配置文件,配置如下:

<?xml version="1.0" encoding="utf-8" ?>
03       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值