zoukankan      html  css  js  c++  java
  • NHibernate中使用NLog

    话说以前的NHibernate版本与Log4Net紧密集成,在群众们千乎万唤之下终于将Log4Net下分离了出来,在NH3本中引入了ILoggerFactory接口,现在我们可以使用其它的Log框架作为NH3的日志记录工具。现在我打算将NLog作为NHibernate的日志工具。
    这里提供本文章的代码:代码下载
    废话少说,直奔主题,通过以下三步就可以让NHibernate 3使用NLog:
    1.提供一个自定义的继承自ILoggerFactory的类。
    2.在App.config或Web.config中使用自定义的ILoggerFactory。
    3.配置NLog。
    先看一下ILoggerFactory接口,该接口返回IInternalLogger类:
    image
    image
    创建一个名为NHibernate.Logging的工程
    image

    添加NHibernate和NLog的引用:
    image
    添加两个类: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程序集
    image
    好,现在我们已经完成了基本的工作,接下来只需要象平时一样增加NHibernate的配置文件和NLog的配置文件既可。
    image
    image
    不要忘记将这两个配置文件的属性都设置成始终复制:
    image
    一切准备就绪,最后在App.config(ASP.NET 中是web.config)中加入如下代码就可以工作了:
    关于为什么要加入nhibernate-logger配置节,请参见我另一篇文章《关于《NHibernate中使用NLog》中在App.config(web.config)中增加nhibernate-logger节点的疑惑
    image
    运行后就可以看到Console有相应的日志输出,同时在bin中也有nlog.log和nhibernate.log两个日志文件输出。现在这两位同志终于可以并肩做战了,哈哈
  • 相关阅读:
    Windows 8 系列(二):Metro Style 应用程序生命周期(Metro Style Application Life Cycle)
    Windows 8 系列(十):关于AppBar持久显示的相关问题
    Win8 博客园MX应用隐私声明
    Windows 8 系列(四):Win8 RSA加密相关问题
    Windows 8 系列(五):Windows App Cer Kit(Certification Kit)的使用与相关问题
    Windows 8 系列(八):Win8无法获取机器唯一标识的替代方案
    Windows 8 系列(三):挂起管理(Suspension Management )
    java方法
    oracle 10gR2 sql查询性能相关摘要
    IE6,7兼容
  • 原文地址:https://www.cnblogs.com/biyusoft/p/3432068.html
Copyright © 2011-2022 走看看