NLog 2.0.0.2000 使用实例
原文地址:http://www.cnblogs.com/sorex/archive/2013/01/31/2887174.html
--------------------------------------------------------------------------------------------
每条跟踪信息都包含一个记录等级(log level)信息,用来描述该条信息的重要性。NLog支持如下几种记录等级:
- Trace - 最常见的记录信息,一般用于普通输出
- Debug - 同样是记录信息,不过出现的频率要比Trace少一些,一般用来调试程序
- Info - 信息类型的消息
- Warn - 警告信息,一般用于比较重要的场合
- Error - 错误信息
- Fatal - 致命异常信息。一般来讲,发生致命异常之后程序将无法继续执行。
--------------------------------------------------------------------------------------------
之前一直都是使用log4net,但是那令人生畏的维护速度,还是令我转向了NLog。首先我不确定各版本的差异,所以这里仅仅以我用的版本来写。其次,本文以基本应用为基准,不涉及复杂的配置方案。
本文地址http://www.cnblogs.com/sorex/archive/2013/01/31/2887174.html
O、情景(设想情景,各位请按自己需求进行变更)
1.在Logs文件夹下,分日期文件夹记录每日的错误信息。
2.在日期文件夹下,有All.log记录全部错误信息以及UI.log、BLL.log、DAL.log三个日志文件分别记录错误信息。
3.同时在一个单独的MySQL数据库ProjectLogDB中的Logs表中记录错误信息。
4.错误信息包含发生错误的时间、错误级别、堆栈信息、发生错误的方法、源文件路径及行号、错误内容、错误分类(UI、BLL、DAL)。
ok就是上面这些设定,没有其他的神马了。
一、在项目中加入NLog的引用
打开NuGet搜索NLog安装下面的4个

安装完成后会在项目中出现如下2个文件

二、设置日志记录
1.打开NLog.config文件,首先我们搞的All.log
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?xml version="1.0" encoding="utf-8" ?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- 定义参数: ${basedir}:系统路径 ${shortdate}:短日期 yyyy-MM-dd(例:2013-01-31) ${basedir}/Logs/${shortdate}:即为在系统路径下的Logs文件夹下面的日期文件夹--> <variable name="logDirectory" value="${basedir}/Logs/${shortdate}"/> <targets> <!-- 定义输出模板: type="File":这个记录方式为文件类型 fileName="${logDirectory}/All.log":表示输出到文件All.log中 layout="...":输出文件中错误的显示格式 ${logDirectory}:为上述定义的路径 ${longdate}:输出长日期 yyyy-MM-dd HH:mm:ss.ffff(例:2013-01-31 14:49:21.2120) ${level}:错误等级(由低到高为Trace,Debug,Info,Warn,Error,Fatal) ${newline}:输出 新的一行 ${stacktrace}:输出 堆栈信息 ${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}:输出 命名空间.类名.方法名(文件路径:行号) ${message}:输出错误信息--> <target xsi:type="File" name="AllFile" fileName="${logDirectory}/All.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> </targets> <rules> <!-- 定义输出日志: name="*":记录所有信息 minlevel="Trace":记录的最低错误级别为Trace writeTo="AllFile":日志写入AllFile的target中--> <logger name="*" minlevel="Trace" writeTo="AllFile" /> </rules></nlog> |
2.将上面的AllFile的target复制3次修改名字为UI、BLL、DAL,添加相应的logger如下代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?xml version="1.0" encoding="utf-8" ?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- 定义参数: ${basedir}:系统路径 ${shortdate}:短日期 yyyy-MM-dd(例:2013-01-31) ${basedir}/Logs/${shortdate}:即为在系统路径下的Logs文件夹下面的日期文件夹--> <variable name="logDirectory" value="${basedir}/Logs/${shortdate}"/> <targets> <!-- 定义输出模板: type="File":这个记录方式为文件类型 fileName="${logDirectory}/All.log":表示输出到文件All.log中 layout="...":输出文件中错误的显示格式 ${logDirectory}:为上述定义的路径 ${longdate}:输出长日期 yyyy-MM-dd HH:mm:ss.ffff(例:2013-01-31 14:49:21.2120) ${level}:错误等级(由低到高为Trace,Debug,Info,Warn,Error,Fatal) ${newline}:输出 新的一行 ${stacktrace}:输出 堆栈信息 ${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}:输出 命名空间.类名.方法名(文件路径:行号) ${message}:输出错误信息--> <target xsi:type="File" name="AllFile" fileName="${logDirectory}/All.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="UI" fileName="${logDirectory}/UI.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="BLL" fileName="${logDirectory}/BLL.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="DAL" fileName="${logDirectory}/DAL.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> </targets> <rules> <!-- 定义输出日志: name="*":记录所有信息 minlevel="Trace":记录的最低错误级别为Trace writeTo="AllFile":日志写入AllFile的target中--> <logger name="*" minlevel="Trace" writeTo="AllFile" /> <!-- 定义输出日志: name="*.UI.*":记录包含.UI.的命名空间的所有信息(第一个*最好替换为固定的,例如我的UI层命名空间为J.UI那么这里就可以写J.UI.*)--> <logger name="*.UI.*" minlevel="Trace" writeTo="UI" /> <logger name="*.BLL.*" minlevel="Trace" writeTo="BLL" /> <logger name="*.DAL.*" minlevel="Trace" writeTo="DAL" /> </rules></nlog> |
3.到这里我们创建一个Class1来测试下,文件内容如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using NLog;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace J.BLL{ public class Class1 { private static Logger logger = LogManager.GetCurrentClassLogger(); public void Test() { logger.Log(LogLevel.Debug, "this is at BLL Error"); } }} |
4.我们得到的错误信息如下
|
1
2
3
4
5
6
|
2013-01-31 14:49:21.3590 ■Debug ▲<no type>.lambda_method => HomeController.Index => Class1.Test ◇J.BLL.Class1.Test(d:\ProjectTest\J.BLL\Class1.cs:16) ◆this is at BLL Error*************************************************************************** |
5.输出到文件目前没有问题了,下面就来配置如何连接到MySQL,首先在ProjectLogDB中创建表Logs
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
-- ------------------------------ Table structure for `Logs`-- ----------------------------DROP TABLE IF EXISTS `Logs`;CREATE TABLE `Logs` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `CreateDate` datetime NOT NULL, `LogLevel` varchar(5) NOT NULL, `CallSite` varchar(5000) DEFAULT NULL, `Massage` longtext, `StackTrace` varchar(5000) DEFAULT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; |
6.修改NLog的配置文件如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<?xml version="1.0" encoding="utf-8" ?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- 定义参数: ${basedir}:系统路径 ${shortdate}:短日期 yyyy-MM-dd(例:2013-01-31) ${basedir}/Logs/${shortdate}:即为在系统路径下的Logs文件夹下面的日期文件夹--> <variable name="logDirectory" value="${basedir}/Logs/${shortdate}"/> <targets> <!-- 定义输出模板: type="File":这个记录方式为文件类型 fileName="${logDirectory}/All.log":表示输出到文件All.log中 layout="...":输出文件中错误的显示格式 ${logDirectory}:为上述定义的路径 ${longdate}:输出长日期 yyyy-MM-dd HH:mm:ss.ffff(例:2013-01-31 14:49:21.2120) ${level}:错误等级(由低到高为Trace,Debug,Info,Warn,Error,Fatal) ${newline}:输出 新的一行 ${stacktrace}:输出 堆栈信息 ${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}:输出 命名空间.类名.方法名(文件路径:行号) ${message}:输出错误信息--> <target xsi:type="File" name="AllFile" fileName="${logDirectory}/All.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="UI" fileName="${logDirectory}/UI.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="BLL" fileName="${logDirectory}/BLL.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="DAL" fileName="${logDirectory}/DAL.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <!-- 定义输出到MySQL中: type="Database":这个记录方式是数据库 dbProvider="MySql.Data.MySqlClient":使用MySQL的连接方式 connectionString="Server=XXX.XXX.XXX.XXX;Database=ProjectLogDB;Uid=XXX;Pwd=XXX;":数据库的连接字符串 commandText="insert into Logs(CreateDate,LogLevel,CallSite,Massage,StackTrace) values (@CreateDate,@LogLevel,@CallSite,@Massage,@StackTrace)":insert语句 <parameter name="CreateDate" layout="${longdate}" />对应到insert语句的参数的值--> <target xsi:type="Database" name="AllDatabase" dbProvider="MySql.Data.MySqlClient" connectionString="Server=XXX.XXX.XXX.XXX;Database=ProjectLogDB;Uid=XXX;Pwd=XXX;" commandText="insert into Logs(CreateDate,LogLevel,CallSite,Massage,StackTrace) values (@CreateDate,@LogLevel,@CallSite,@Massage,@StackTrace)"> <parameter name="CreateDate" layout="${longdate}" /> <parameter name="LogLevel" layout="${level}" /> <parameter name="CallSite" layout="${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}" /> <parameter name="Massage" layout="${message}" /> <parameter name="StackTrace" layout="${stacktrace}" /> </target> </targets> <rules> <!-- 定义输出日志: name="*":记录所有信息 minlevel="Trace":记录的最低错误级别为Trace writeTo="AllFile,AllDatabase":日志写入AllFile和AllDatabase的target中--> <logger name="*" minlevel="Trace" writeTo="AllFile,AllDatabase" /> <!-- 定义输出日志: name="*.UI.*":记录包含.UI.的命名空间的所有信息(第一个*最好替换为固定的,例如我的UI层命名空间为J.UI那么这里就可以写J.UI.*)--> <logger name="*.UI.*" minlevel="Trace" writeTo="UI" /> <logger name="*.BLL.*" minlevel="Trace" writeTo="BLL" /> <logger name="*.DAL.*" minlevel="Trace" writeTo="DAL" /> </rules></nlog> |
7.完整的无说明的配置文件如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<?xml version="1.0" encoding="utf-8" ?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <variable name="logDirectory" value="${basedir}/Logs/${shortdate}"/> <targets> <target xsi:type="File" name="AllFile" fileName="${logDirectory}/All.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="UI" fileName="${logDirectory}/UI.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="BLL" fileName="${logDirectory}/BLL.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="File" name="DAL" fileName="${logDirectory}/DAL.log" layout="${longdate} ■${level}${newline} ▲${stacktrace}${newline} ◇${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline} ◆${message}${newline}${newline}***************************************************************************" /> <target xsi:type="Database" name="AllDatabase" dbProvider="MySql.Data.MySqlClient" connectionString="Server=XXX.XXX.XXX.XXX;Database=ProjectLogDB;Uid=XXX;Pwd=XXX;" commandText="insert into Logs(CreateDate,LogLevel,CallSite,Massage,StackTrace) values (@CreateDate,@LogLevel,@CallSite,@Massage,@StackTrace)"> <parameter name="CreateDate" layout="${longdate}" /> <parameter name="LogLevel" layout="${level}" /> <parameter name="CallSite" layout="${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}" /> <parameter name="Massage" layout="${message}" /> <parameter name="StackTrace" layout="${stacktrace}" /> </target> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="AllFile,AllDatabase" /> <logger name="*.UI.*" minlevel="Trace" writeTo="UI" /> <logger name="*.BLL.*" minlevel="Trace" writeTo="BLL" /> <logger name="*.DAL.*" minlevel="Trace" writeTo="DAL" /> </rules></nlog> |
8.数据库中记录如下

三、总结
NLog其实也是一个用起来蛮简单的工具,复杂的功能请到官网查询,以上示例仅供日常使用。
NLog 2.0.0.2000 使用实例的更多相关文章
- [ActionScript 3.0] Away3D 官网实例
/* Dynamic tree generation and placement in a night-time scene Demonstrates: How to create a height ...
- JAX-RS 2.0 REST客户端编程实例
JAX-RS 2.0 REST客户端编程实例 2014/01/28 | 分类: 基础技术, 教程 | 0 条评论 | 标签: JAX-RS, RESTFUL 分享到:3 本文由 ImportNew - ...
- Asp.Net MVC2.0 Url 路由入门---实例篇
本篇主要讲述Routing组件的作用,以及举几个实例来学习Asp.Net MVC2.0 Url路由技术. 接着上一篇开始讲,我们在Global.asax中注册一条路由后,我们的请求是怎么转到相应的Vi ...
- 《Drools7.0.0.Final规则引擎教程》第3章 3.1 Hello World 实例
3.1 Hello World 实例 在上一章中介绍了Drools5x版本中规则引擎使用的实例,很明显在Drools7中KnowledgeBase类已经标注为"@Deprecated&quo ...
- NPOI2.2.0.0实例详解(十)—设置EXCEL单元格【文本格式】 NPOI 单元格 格式设为文本 HSSFDataFormat
NPOI2.2.0.0实例详解(十)—设置EXCEL单元格[文本格式] 2015年12月10日 09:55:17 阅读数:3150 using System; using System.Collect ...
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
Oracle 远程访问配置 服务端配置 如果不想自己写,可以通过 Net Manager 来配置. 以下配置文件中的 localhost 改为 ip 地址,否则,远程不能访问. 1.网络监听配置 ...
- yii2.0增删改查实例讲解
yii2.0增删改查实例讲解一.创建数据库文件. 创建表 CREATE TABLE `resource` ( `id` int(10) NOT NULL AUTO_INCREMENT, `textur ...
- NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片
--------------------- 本文来自 天水宇 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/xxs77ch/article/details/50553 ...
- NPOI2.2.0.0实例详解(八)—设置EXCEL单元格【数字格式】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- [ActionScript 3.0] 动态绘制扇形实例(拖拽绘制)
package { import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; / ...
随机推荐
- bzoj1145
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1145 神题...... 定义f(abcd)为高度排名为abcd的个数,例如闪电的个数为f(13 ...
- Android实现摇晃手机的监听
摘自:http://blog.csdn.net/xwren362922604/article/details/8515343 监听摇晃手机的类: /** * @author renxinwei ...
- 栈和托管堆/值类型和引用类型/强制类型转换/装箱和拆箱[C#]
原文地址:http://www.cnblogs.com/xy8.cn/articles/1227228.html 一.栈和托管堆 通用类型系统(CTS)区分两种基本类型:值类型和引用类型.它 ...
- hdu4641-K-string(后缀自动机)
Problem Description Given a string S. K-string is the sub-string of S and it appear in the S at leas ...
- EucOne调试
~/.ssh/id_rsa权限问题,造成ssh无法登陆问题解决;
- WPF - XAML如何引入名字空间
WPF 的XAML引入名字空间的概念,经常容易让人混淆.如何引入名字空间,并且在XAML中调用其中的类,下面给一个简单的介绍. 比如我们有一个Hepler类. namespace Wheat.PIMS ...
- 线程、线程句柄、线程ID
什么是句柄:句柄是一种指向指针的指针.我们知道,所谓指针是一种内存地址.应用程序启动后,组成这个程序的各对象是住留在内存的.如果简单地理解,似乎我们只要获知这个内存的首地址,那么就可以随时用这个地址 ...
- .NET开发人员必须知道的八个网站
对于不熟悉.NET技术的朋友,需要说明一下,.NET提供了一个平台和一些相应的工具,.NET开发人员可以使用它们来在开发Windows桌面,互联网,甚至是手持移动设备上构建极富交互性的应用.很有可能你 ...
- 配置错误--分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。
在部署网站的时候,很容易遇到这个一样错误:分析器错误消息: 无法识别的属性“targetFramework”.请注意属性名称区分大小写. 错误如图: 错误原因: 部署网站时,使用的应用程序池版本不对 ...
- ASP.NET 动态编译、预编译和 WebDeployment 项目(转)
概述 在 Web 服务器上,既可以部署源文件,也可以部署编译后程序集. 若部署源文件,则当用户访问时,Web 应用程序会被动态编译,并缓存该程序集,以便下次访问. 否则,若部署程序集,Web 应用程序 ...