NHibernate.Glimpse会覆盖log4net

时间:2013-11-26 17:18:29

标签: c# .net nhibernate log4net glimpse

Log4net只生成一个空的日志文件。 NHibernate消息未被记录。我很难弄明白为什么。事实证明,现在这是一个NHibernate.Glimpse问题......

以下设置会覆盖日志提供程序:

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

一旦出现此设置,(log4net)日志文件就不再接收NHibernate消息。如果没有,Glimpse将不会依次显示NHibernate细节。

有没有人能想到一种方法让Glimpse显示NHibernate信息,同时仍然根据log4net配置进行记录?

1 个答案:

答案 0 :(得分: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 ));
    }
}