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" ?> |
07 |
for information on customizing logging rules and outputs. |
10 |
<!-- add your targets here --> |
13 |
<target xsi:type= "File" name= "f" fileName= "${basedir}/logs/${shortdate}.log" |
14 |
layout= "${longdate} ${uppercase:${level}} ${message}" /> |
19 |
<!-- add your logging rules here --> |
22 |
<logger name= "*" minlevel= "Trace" writeTo= "f" /> |
或者如果你是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" ?> |