NHibernate中使用NLog
話說以前的NHibernate版本與Log4Net緊密集成,在群眾們千乎萬喚之下終于將Log4Net下分離了出來,在NH3本中引入了ILoggerFactory接口,現(xiàn)在我們可以使用其它的Log框架作為NH3的日志記錄工具。現(xiàn)在我打算將NLog作為NHibernate的日志工具。
這里提供本文章的代碼:代碼下載
這里提供本文章的代碼:代碼下載
廢話少說,直奔主題,通過以下三步就可以讓NHibernate 3使用NLog:
1.提供一個(gè)自定義的繼承自ILoggerFactory的類。
2.在App.config或Web.config中使用自定義的ILoggerFactory。
3.配置NLog。
2.在App.config或Web.config中使用自定義的ILoggerFactory。
3.配置NLog。
創(chuàng)建一個(gè)名為NHibernate.Logging的工程
添加NHibernate和NLog的引用:
添加兩個(gè)類:NLogFactory和NLogLogger。這兩個(gè)類分別實(shí)現(xiàn)ILoggerFactory和IInternalLogger接口。代碼如下:
NLogFactory.cs
NLogLogger.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 }
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 }
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程序集
好,現(xiàn)在我們已經(jīng)完成了基本的工作,接下來只需要象平時(shí)一樣增加NHibernate的配置文件和NLog的配置文件既可。
不要忘記將這兩個(gè)配置文件的屬性都設(shè)置成始終復(fù)制:

好,現(xiàn)在我們已經(jīng)完成了基本的工作,接下來只需要象平時(shí)一樣增加NHibernate的配置文件和NLog的配置文件既可。
不要忘記將這兩個(gè)配置文件的屬性都設(shè)置成始終復(fù)制:

一切準(zhǔn)備就緒,最后在App.config(ASP.NET 中是web.config)中加入如下代碼就可以工作了:
關(guān)于為什么要加入nhibernate-logger配置節(jié),請(qǐng)參見我另一篇文章《關(guān)于《NHibernate中使用NLog》中在App.config(web.config)中增加nhibernate-logger節(jié)點(diǎn)的疑惑》
運(yùn)行后就可以看到Console有相應(yīng)的日志輸出,同時(shí)在bin中也有nlog.log和nhibernate.log兩個(gè)日志文件輸出。現(xiàn)在這兩位同志終于可以并肩做戰(zhàn)了,哈哈
運(yùn)行后就可以看到Console有相應(yīng)的日志輸出,同時(shí)在bin中也有nlog.log和nhibernate.log兩個(gè)日志文件輸出。現(xiàn)在這兩位同志終于可以并肩做戰(zhàn)了,哈哈





浙公網(wǎng)安備 33010602011771號(hào)