gpt4 book ai didi

c# - NHibernate.Glimpse 覆盖 log4net

转载 作者:行者123 更新时间:2023-11-30 20:53:08 27 4
gpt4 key购买 nike

Log4net 只生成了一个空日志文件。 NHibernate 消息没有被记录下来。我很难弄清楚为什么。原来这是一个NHibernate.Glimpse现在问...

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

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

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

谁能想出一种方法让 Glimpse 显示 NHibernate 信息,同时仍然根据 log4net 配置进行日志记录?

最佳答案

按照@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 ));
}
}

关于c# - NHibernate.Glimpse 覆盖 log4net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20223738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com