我最近将log4net日志记录从使用配置文件切换到以编程方式设置。这导致nhibrate条目重复2次或有时3次。这是代码。它使用的字符串看起来像“logger1 | debug,logger2 | info”
private void SetupLog4netLoggers()
{
IAppender appender = GetAppender();
SetupRootLogger(appender);
foreach (string logger in Loggers)
{
CommaStringList parts = new CommaStringList(logger, '|');
if (parts.Count != 2)
continue;
AddLogger(parts[0], parts[1], appender);
}
log.Debug("Log4net has been setup");
}
private IAppender GetAppender()
{
RollingFileAppender appender = new RollingFileAppender();
appender.File = LogFile;
appender.AppendToFile = true;
appender.MaximumFileSize = MaximumFileSize;
appender.MaxSizeRollBackups = MaximumBackups;
PatternLayout layout = new PatternLayout(PATTERN);
layout.ActivateOptions();
appender.Layout = layout;
appender.ActivateOptions();
return appender;
}
private void SetupRootLogger(IAppender appender)
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders();
hierarchy.Root.AddAppender(appender);
hierarchy.Root.Level = GetLevel(RootLevel);
hierarchy.Configured = true;
log.Debug("Root logger setup, level[" + RootLevel + "]");
}
private void AddLogger(string name, string level, IAppender appender)
{
Logger logger = LogManager.GetRepository().GetLogger(name)as Logger;
if (logger == null)
return;
logger.Level = GetLevel(level);
logger.Additivity = false;
logger.RemoveAllAppenders();
logger.AddAppender(appender);
log.Debug("logger[" + name + "] added, level[" + level + "]");
}
这是我们在日志中看到的一个例子......
2010-05-06 15:50:39,781 [1] DEBUG NHibernate.Impl.SessionImpl - running ISession.Dispose()
2010-05-06 15:50:39,781 [1] DEBUG NHibernate.Impl.SessionImpl - closing session
2010-05-06 15:50:39,781 [1] DEBUG NHibernate.AdoNet.AbstractBatcher - running BatcherImpl.Dispose(true)
2010-05-06 15:50:39,796 [1] DEBUG NHibernate.Impl.SessionImpl - running ISession.Dispose()
2010-05-06 15:50:39,796 [1] DEBUG NHibernate.Impl.SessionImpl - closing session
2010-05-06 15:50:39,796 [1] DEBUG NHibernate.AdoNet.AbstractBatcher - running BatcherImpl.Dispose(true)
2010-05-06 15:50:39,796 [1] DEBUG NHibernate.Impl.SessionImpl - running ISession.Dispose()
2010-05-06 15:50:39,796 [1] DEBUG NHibernate.Impl.SessionImpl - closing session
2010-05-06 15:50:39,796 [1] DEBUG NHibernate.AdoNet.AbstractBatcher - running BatcherImpl.Dispose(true)
欢迎任何提示。
答案 0 :(得分:0)
我不确定发生了什么,但我会尝试删除以下几行:
logger.Additivity = false;
logger.RemoveAllAppenders();
logger.AddAppender(appender);
记录器应该从root appender继承appender。当你以这种方式添加appender时,log4net可能会混淆一些东西,尽管不应该这样......
答案 1 :(得分:0)
我认为答案是 - 它实际上并不是写重复的。没有调试NHibernate代码我不能确定,但我猜它可能只是写出来或反复做同样的事情
我们还是要回到使用配置文件,所以必须看看我是否仍然可以重复。