博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NHibernate中使用NLog
阅读量:6953 次
发布时间:2019-06-27

本文共 3784 字,大约阅读时间需要 12 分钟。

话说以前的NHibernate版本与Log4Net紧密集成,在群众们千乎万唤之下终于将Log4Net下分离了出来,在NH3本中引入了ILoggerFactory接口,现在我们可以使用其它的Log框架作为NH3的日志记录工具。现在我打算将NLog作为NHibernate的日志工具。
这里提供本文章的代码:
废话少说,直奔主题,通过以下三步就可以让NHibernate 3使用NLog:
1.提供一个自定义的继承自ILoggerFactory的类。
2.在App.config或Web.config中使用自定义的ILoggerFactory。
3.配置NLog。
先看一下ILoggerFactory接口,该接口返回IInternalLogger类:
创建一个名为NHibernate.Logging的工程
添加NHibernate和NLog的引用:
添加两个类:NLogFactory和NLogLogger。这两个类分别实现ILoggerFactory和IInternalLogger接口。代码如下:
NLogFactory.cs  
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Configuration;
4
using
NLog;
5
6
namespace
NHibernate.Logging
7
{
8
public
class
NLogFactory : ILoggerFactory
9
{
10
public
IInternalLogger LoggerFor(System.Type type)
11
{
12
return
new
NLogLogger();
13
}
14
15
public
IInternalLogger LoggerFor(
string
keyName)
16
{
17
return
new
NLogLogger();
18
}
19
}
20
}
  
NLogLogger.cs
1
using
System;
2
using
System.Collections.Specialized;
3
using
System.Configuration;
4
using
NLog;
5
6
namespace
NHibernate.Logging
7
{
8
public
class
NLogLogger : IInternalLogger
9
{
10
private
static
readonly
Logger log
=
LogManager.GetCurrentClassLogger();
11
12
public
bool
IsDebugEnabled {
get
;
private
set
; }
13
public
bool
IsErrorEnabled {
get
;
private
set
; }
14
public
bool
IsFatalEnabled {
get
;
private
set
; }
15
public
bool
IsInfoEnabled {
get
;
private
set
; }
16
public
bool
IsWarnEnabled {
get
;
private
set
; }
17
18
public
NLogLogger()
19
{
20
IsErrorEnabled
=
true
;
21
IsFatalEnabled
=
true
;
22
IsWarnEnabled
=
true
;
23
IsDebugEnabled
=
true
;
24
IsInfoEnabled
=
true
;
25
}
26
27
public
void
Debug(
object
message, Exception exception)
28
{
29
if
(IsDebugEnabled)
30
log.DebugException(message.ToString(), exception);
31
}
32
33
public
void
Debug(
object
message)
34
{
35
if
(IsDebugEnabled)
36
log.Debug(message.ToString());
37
}
38
39
public
void
DebugFormat(
string
format,
params
object
[] args)
40
{
41
if
(IsDebugEnabled)
42
log.Debug(
string
.Format(format, args));
43
}
44
45
public
void
Error(
object
message, Exception exception)
46
{
47
if
(IsErrorEnabled)
48
log.ErrorException(message.ToString(), exception);
49
}
50
51
public
void
Error(
object
message)
52
{
53
if
(IsErrorEnabled)
54
log.Error(message.ToString());
55
}
56
57
public
void
ErrorFormat(
string
format,
params
object
[] args)
58
{
59
if
(IsErrorEnabled)
60
log.Error(
string
.Format(format, args));
61
}
62
63
public
void
Fatal(
object
message, Exception exception)
64
{
65
if
(IsFatalEnabled)
66
log.Fatal(message.ToString(), exception);
67
}
68
69
public
void
Fatal(
object
message)
70
{
71
if
(IsFatalEnabled)
72
log.Fatal(message.ToString());
73
}
74
75
public
void
Info(
object
message, Exception exception)
76
{
77
if
(IsInfoEnabled)
78
log.Info(message.ToString(), exception);
79
}
80
81
public
void
Info(
object
message)
82
{
83
if
(IsInfoEnabled)
84
log.Info(message.ToString());
85
}
86
87
public
void
InfoFormat(
string
format,
params
object
[] args)
88
{
89
if
(IsInfoEnabled)
90
log.Info(
string
.Format(format, args));
91
}
92
93
public
void
Warn(
object
message, Exception exception)
94
{
95
if
(IsWarnEnabled)
96
log.WarnException(message.ToString(), exception);
97
}
98
99
public
void
Warn(
object
message)
100
{
101
if
(IsWarnEnabled)
102
log.Warn(message.ToString());
103
}
104
105
public
void
WarnFormat(
string
format,
params
object
[] args)
106
{
107
if
(IsWarnEnabled)
108
log.Warn(
string
.Format(format, args));
109
}
110
}
111
}
 
在主程序中引用该程序集和NHibernate与NLog程序集
好,现在我们已经完成了基本的工作,接下来只需要象平时一样增加NHibernate的配置文件和NLog的配置文件既可。
不要忘记将这两个配置文件的属性都设置成始终复制:
一切准备就绪,最后在App.config(ASP.NET 中是web.config)中加入如下代码就可以工作了:
关于为什么要加入nhibernate-logger配置节,请参见我另一篇文章《 》
运行后就可以看到Console有相应的日志输出,同时在bin中也有nlog.log和nhibernate.log两个日志文件输出。现在这两位同志终于可以并肩做战了,哈哈

转载于:https://www.cnblogs.com/biyusoft/archive/2011/07/10/3432068.html

你可能感兴趣的文章
背景-需要-需求规格
查看>>
4.6. CSS Sprite
查看>>
博客园首页博问闪存新随笔联系订阅管理 随笔- 252 文章- 0 评论- 45 HashPasswordForStoringInConfigFile中的Md5算法并非常用的Md5算法...
查看>>
OpenGL入门笔记(十三)
查看>>
Web趋势地图
查看>>
Python天天美味(11) - 可爱的大小写
查看>>
单链表
查看>>
WF4:AcitivityAction和ActivityFunc
查看>>
诺基亚S40手机联系人导入安卓手机
查看>>
JS魔法堂:再识Bitwise Operation & Bitwise Shift
查看>>
Vue.js——60分钟快速入门
查看>>
WordPress动作钩子函数add_action()、do_action()源码解析
查看>>
javaweb学习总结(三十二)——JDBC学习入门
查看>>
基于mapreducer的图算法
查看>>
[LeetCode] Kth Largest Element in an Array
查看>>
CTF---Web入门第七题 猫抓老鼠
查看>>
用XAML做网页!!—边栏与页脚
查看>>
Arduino单片机使用和开发问题记录
查看>>
Mac 升级 OS X 10.8.2 后 VirtualBox 无法启动的问题
查看>>
Js~在文件中引入其它相关的JS文件
查看>>