Log4net 重复记录整体
我最近将 log4net 日志记录从使用配置文件切换为以编程方式设置。这导致 nhiberate 条目重复 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)
欢迎任何提示。
I recently switched out log4net logging from using config files to being set up programmatically. This has resulted in the nhiberate entries getting repeated 2 or sometimes 3 times. Here's the code. It uses a string which looks something like this "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 + "]");
}
And here's an example of what we see in our logs...
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)
Any hints welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定发生了什么,但我会尝试删除以下行:
记录器无论如何都应该从根附加器继承附加器。当您以这种方式添加附加程序时,也许 log4net 会混淆一些东西,尽管情况不应该如此......
I am not really sure what is happening, but I would try to remove the following lines:
The loggers should inherit the appender anyway from the root appender. Maybe log4net mixes up something when you add appenders in this fashion although that should not be the case...
我认为答案是——它实际上并不是在写重复的内容。如果不调试 NHibernate 代码,我无法确定,但我猜测它可能只是将其写出来或重复执行相同的操作。
无论如何,我们都会回到使用配置文件,所以必须看看我是否仍然得到可能的重复项。
I think the answer is - its not in fact writing duplicates. Without debugging the NHibernate code I can't be sure but I'm guessing its probably just writing it out or doing same thing repeatedly
We're going back to using config files anyway so will have to see if I still get possible duplicates.