Log4net 重复记录整体

发布于 2024-08-31 09:36:25 字数 2765 浏览 7 评论 0原文

我最近将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-09-07 09:36:25

我不太确定发生了什么,但我会尝试删除以下行:

logger.Additivity = false;
logger.RemoveAllAppenders();
logger.AddAppender(appender);

记录器无论如何都应该从根附加器继承附加器。当您以这种方式添加附加程序时,也许 log4net 会混淆一些东西,尽管情况不应该如此......

I am not really sure what is happening, but I would try to remove the following lines:

logger.Additivity = false;
logger.RemoveAllAppenders();
logger.AddAppender(appender);

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...

杀手六號 2024-09-07 09:36:25

我认为答案是——它实际上并不是在写重复的内容。如果不调试 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文