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; / ...
随机推荐
- cf443A Anton and Letters
A. Anton and Letters time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- POJ1845 数论 二分快速取余
大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题思路: 应用定理主要有三个: (1) 整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. ...
- 旋的X-Di
旋的X-Di | 氪加 旋的X-Di
- How to face setbacks
I’ve been in a bad mood since I started on the American Accent. I became even more upset when I adde ...
- IIS PHP 配置 问题总结
今天帮助朋友解决一个IIS配置PHP的问题.大概是这样子的. IIS 与 PHP配置好了之后不能訪问,出现例如以下错误: HTTP 错误 500.19 - Internal Server Error ...
- 浪潮服务器通过ipmitool获取mac地址
一.GPU服务器 #配置两个主板集成千兆四个外插PCI万兆网卡# 板载网卡可以使用命令获取到:RAW 0X30 0X21 就可以读取到第一块网卡的MAC,就是以下返回值的后6位. 0c,c4,7a,5 ...
- 使用RDS不得不知的注意事项
使用RDS不得不知的注意事项 1.RDS实例升级需要注意的事项 RDS在进行实例升级的过程中会出现最长30秒左右的连接闪断,需要您提前做好准备,并设置好程序跟RDS的自动重连,避免因为升级的闪断导致您 ...
- 设置单选的listView或者gridview
主要是这个BeaseAdapter的方法notifyDataSetChanged()的使用;作用 :调用BaseAdapter中的getView();方法,刷新ListView中的数据.实现:1.在B ...
- js获取get值
//获取get值 function getPar(par) { //获取当前URL var local_url = document.location.href; //获取要取得的get参数位置 va ...
- WCF入门教程系列六
一.前言 前面的几个章节介绍了很多理论基础,如:什么是WCF.WCF中的A.B.C.WCF的传输模式.本文从零开始和大家一起写一个小的WCF应用程序Demo. 大多框架的学习都是从增.删.改.查开始来 ...