2013-11-26 69 views
2

Log4net只產生一個空的日誌文件。 NHibernate消息沒有被記錄。我很難找出原因。事實證明,這是一個NHibernate.Glimpse現在的問題...NHibernate.Glimpse覆蓋log4net

下面的設置將覆蓋日誌提供:一旦

<add key="nhibernate-logger" 
    value="NHibernate.Glimpse.LoggerFactory, NHibernate.Glimpse"/> 

這項設定,目前(log4net的)日誌文件沒有收到消息的NHibernate了。如果沒有,Glimpse將不會顯示NHibernate的細節。

任何人都可以想辦法讓Glimpse顯示NHibernate信息,同時仍然根據log4net配置進行日誌記錄嗎?

+2

我認爲你可以嘗試寫一個由Glimpse和log4net組成的複合記錄器 – wiero

回答

4

按照@wiero的建議創建複合記錄器工廠和記錄器。

public class CombinedLoggerFactory : ILoggerFactory 
{ 
    IList<ILoggerFactory> factories = new List<ILoggerFactory> { 
     new Log4NetLoggerFactory(), 
     new NHibernate.Glimpse.LoggerFactory() 
    }; 

    public CombinedLoggerFactory() 
    { 
    } 

    public IInternalLogger LoggerFor(Type type) 
    { 
     return new CombinedLogger(factories.Select(f => f.LoggerFor(type))); 
    } 

    public IInternalLogger LoggerFor(string keyName) 
    { 
     return new CombinedLogger(factories.Select(f => f.LoggerFor(keyName))); 
    } 
} 

internal static class IEnumerableExtensions 
{ 
    internal static void ForEach<T>(this IEnumerable<T> list, Action<T> action) 
    { 
     foreach(var x in list) { 
      action(x); 
     } 
    } 
} 

internal class CombinedLogger : IInternalLogger 
{ 
    readonly IList<IInternalLogger> loggers; 

    internal CombinedLogger(IEnumerable<IInternalLogger> logs) 
    { 
     loggers = logs.ToList(); 
    } 

    public void Debug(object message, Exception exception) 
    { 
     loggers.Where(l => l.IsDebugEnabled) 
      .ForEach(l => l.Debug(message, exception)); 
    } 

    public void Debug(object message) 
    { 
     loggers.Where(l => l.IsDebugEnabled) 
      .ForEach(l => l.Debug(message)); 
    } 

    public void DebugFormat(string format, params object[] args) 
    { 
     loggers.ForEach(l => l.DebugFormat(format, args)); 
    } 

    public void Error(object message, Exception exception) 
    { 
     loggers.Where(l => l.IsErrorEnabled) 
      .ForEach(l => l.Error(message, exception)); 
    } 

    public void Error(object message) 
    { 
     loggers.Where(l => l.IsErrorEnabled) 
      .ForEach(l => l.Error(message)); 
    } 

    public void ErrorFormat(string format, params object[] args) 
    { 
     loggers.ForEach(l => l.ErrorFormat(format, args)); 
    } 

    public void Fatal(object message, Exception exception) 
    { 
     loggers.Where(l => l.IsFatalEnabled) 
      .ForEach(l => l.Fatal(message, exception)); 
    } 

    public void Fatal(object message) 
    { 
     loggers.Where(l => l.IsFatalEnabled) 
      .ForEach(l => l.Fatal(message)); 
    } 

    public void Info(object message, Exception exception) 
    { 
     loggers.Where(l => l.IsInfoEnabled) 
      .ForEach(l => l.Info(message, exception)); 
    } 

    public void Info(object message) 
    { 
     loggers.Where(l => l.IsInfoEnabled) 
      .ForEach(l => l.Info(message)); 
    } 

    public void InfoFormat(string format, params object[] args) 
    { 
     loggers.ForEach(l => l.InfoFormat(format, args)); 
    } 

    public bool IsDebugEnabled 
    { 
     get { return loggers.Any(l => l.IsDebugEnabled); } 
    } 

    public bool IsErrorEnabled 
    { 
     get { return loggers.Any(l => l.IsErrorEnabled); } 
    } 

    public bool IsFatalEnabled 
    { 
     get { return loggers.Any(l => l.IsFatalEnabled); } 
    } 

    public bool IsInfoEnabled 
    { 
     get { return loggers.Any(l => l.IsInfoEnabled); } 
    } 

    public bool IsWarnEnabled 
    { 
     get { return loggers.Any(l => l.IsWarnEnabled); } 
    } 

    public void Warn(object message, Exception exception) 
    { 
     loggers.Where(l => l.IsWarnEnabled) 
      .ForEach(l => l.Warn(message, exception)); 
    } 

    public void Warn(object message) 
    { 
     loggers.Where(l => l.IsWarnEnabled) 
      .ForEach(l => l.Warn(message)); 
    } 

    public void WarnFormat(string format, params object[] args) 
    { 
     loggers.ForEach(l => l.WarnFormat(format, args)); 
    } 
}