Log4net设置了两个文件的日志记录,与c#代码的日志记录级别不同

本文关键字:记录 日志 代码 设置 两个 文件 Log4net | 更新日期: 2023-09-27 18:14:36

这是我的问题,我想通过c#应用程序和log4net登录2个不同的文件(一个用于错误,一个用于普通信息),我在xml中找到了配置,但我想从代码中做到这一点,这是我为设置一个文件的日志发现的源:

 public static void Setup()
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        hierarchy.Root.RemoveAllAppenders();
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.ConversionPattern = "%date THREAD [%thread] %logger: %message%newline"; //%-5level
        patternLayout.ActivateOptions();
        RollingFileAppender infoRoller = new RollingFileAppender();
        infoRoller.AppendToFile = true;
        infoRoller.File = @"logs'";
        infoRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
        infoRoller.MaxSizeRollBackups = 3;
        infoRoller.Layout = patternLayout;
        infoRoller.DatePattern = @"IN'FO'_yyyy-MM-dd.'lo'g";
        infoRoller.StaticLogFileName = false;
        infoRoller.ActivateOptions();
        hierarchy.Root.AddAppender(infoRoller);
        MemoryAppender memory = new MemoryAppender();
        memory.ActivateOptions();
        hierarchy.Root.AddAppender(memory);
        hierarchy.Root.Level = Level.Debug;
        hierarchy.Configured = true;
    }

在另一个类中用于日志记录:

private readonly ILog log = LogManager.GetLogger("GENERAL-LOG");

Logger.Setup();
log.Debug("Configurazioni caricate con successo.");

代码可以工作。

现在,我做了这个改变得到2 log:

    public static void Setup()
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        hierarchy.Root.RemoveAllAppenders();
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.ConversionPattern = "%date THREAD [%thread] %logger: %message%newline"; //%-5level
        patternLayout.ActivateOptions();
        RollingFileAppender infoRoller = new RollingFileAppender();
        infoRoller.AppendToFile = true;
        infoRoller.File = @"logs'";
        infoRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
        infoRoller.MaxSizeRollBackups = 3;
        infoRoller.Layout = patternLayout;
        infoRoller.DatePattern = @"IN'FO'_yyyy-MM-dd.'lo'g";
        infoRoller.StaticLogFileName = false;
        infoRoller.ActivateOptions();
        hierarchy.Root.AddAppender(infoRoller);
        //ADDED CODE
        RollingFileAppender errorRoller = new RollingFileAppender();
        errorRoller.AppendToFile = true;
        errorRoller.File = @"logs'";
        errorRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
        errorRoller.MaxSizeRollBackups = 3;
        errorRoller.Layout = patternLayout;
        errorRoller.DatePattern = @"ERROR_yyyy-MM-dd.'lo'g";
        errorRoller.StaticLogFileName = false;
        errorRoller.ActivateOptions();
        hierarchy.Root.AddAppender(errorRoller);
        MemoryAppender memory = new MemoryAppender();
        memory.ActivateOptions();
        hierarchy.Root.AddAppender(memory);
        hierarchy.Root.Level = Level.Debug;
        hierarchy.Configured = true;
    }
}

from other class:

    private readonly ILog log = LogManager.GetLogger("GENERAL-LOG");
    private readonly ILog elog = LogManager.GetLogger("ERROR-LOG");
    Logger.Setup();
    log.Debug("prova scrittura in file1");
    elog.Error("prova errore scrittura in file2");

result:两个日志文件都用两个消息写入。

请建议,非常感谢!

Log4net设置了两个文件的日志记录,与c#代码的日志记录级别不同

问题是您已将两个appender附加到"root"日志记录器。如果您想要两个记录器,那么您必须将它们附加到特定的记录器。

我已经注释了hierarchy.Root.AddAppender(errorRoller);hierarchy.Root.AddAppender(infoRoller);,因为它不再需要,因为追加者被追加到相关的记录器而不是根。

找到下面的代码。在这里,我已经将log和elog作为参数传递给该方法,并分别添加了相关的追加器。

   private static void Setup2(ILog infoLog, ILog elog)
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        hierarchy.Root.RemoveAllAppenders();
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.ConversionPattern = "%date THREAD [%thread] %logger: %message%newline"; //%-5level
        patternLayout.ActivateOptions();
        RollingFileAppender infoRoller = new RollingFileAppender();
        infoRoller.AppendToFile = true;
        infoRoller.File = @"logs'";
        infoRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
        infoRoller.MaxSizeRollBackups = 3;
        infoRoller.Layout = patternLayout;
        infoRoller.DatePattern = @"IN'FO'_yyyy-MM-dd.'lo'g";
        infoRoller.StaticLogFileName = false;
        infoRoller.ActivateOptions();
        //   hierarchy.Root.AddAppender(infoRoller); //Not appended to the root logger anymore
        //ADDED CODE
        RollingFileAppender errorRoller = new RollingFileAppender();
        errorRoller.AppendToFile = true;
        errorRoller.File = @"logs'";
        errorRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
        errorRoller.MaxSizeRollBackups = 3;
        errorRoller.Layout = patternLayout;
        errorRoller.DatePattern = @"ERROR_yyyy-MM-dd.'lo'g";
        errorRoller.StaticLogFileName = false;
        errorRoller.ActivateOptions();
        //   hierarchy.Root.AddAppender(errorRoller); //Not appended to the root logger anymore
        MemoryAppender memory = new MemoryAppender();
        memory.ActivateOptions();
        hierarchy.Root.AddAppender(memory);
        hierarchy.Root.Level = Level.Debug;
        hierarchy.Configured = true;
        ILog log = infoLog;
        Logger logger = (Logger)log.Logger;
        logger.AddAppender(errorRoller);
        log = elog;
        logger = (Logger)log.Logger;
        logger.AddAppender(infoRoller);
    }

调用方法

    private readonly ILog log = LogManager.GetLogger("GENERAL-LOG");
    private readonly ILog elog = LogManager.GetLogger("ERROR-LOG");
    Logger.Setup2(log,elog);
    log.Debug("prova scrittura in file1");
    elog.Error("prova errore scrittura in file2");

请将Setup()方法相关的代码块替换为下面的代码块

变化:1. 删除了"hierarchy.Root.AddAppender(infoller);"并注释了"hierarchy.Root.AddAppender(errorRoller);"。2. 删除了"MemoryAppender",因为在您的情况下似乎不需要。

public static void Setup()
        {

            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            hierarchy.Root.RemoveAllAppenders();
            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = "%date THREAD [%thread] %logger: %message%newline"; //%-5level
            patternLayout.ActivateOptions();

            ILog log = LogManager.GetLogger("GENERAL-LOG");
            Logger l = (Logger)log.Logger;
            RollingFileAppender infoRoller = new RollingFileAppender();
            infoRoller.AppendToFile = true;
            infoRoller.File = @"logs'";
            infoRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
            infoRoller.MaxSizeRollBackups = 3;
            infoRoller.Layout = patternLayout;
            infoRoller.DatePattern = @"IN'FO'_yyyy-MM-dd.'lo'g";
            infoRoller.StaticLogFileName = false;
            infoRoller.ActivateOptions();
            l.AddAppender(infoRoller);

            //ADDED CODE
            ILog elog = LogManager.GetLogger("ERROR-LOG");
            Logger el = (Logger)elog.Logger;

            RollingFileAppender errorRoller = new RollingFileAppender();
            errorRoller.AppendToFile = true;
            errorRoller.File = @"logs'";
            errorRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
            errorRoller.MaxSizeRollBackups = 3;
            errorRoller.Layout = patternLayout;
            errorRoller.DatePattern = @"ERROR_yyyy-MM-dd.'lo'g";
            errorRoller.StaticLogFileName = false;
            errorRoller.ActivateOptions();
            el.AddAppender(errorRoller);
            //hierarchy.Root.AddAppender(errorRoller);
            MemoryAppender memory = new MemoryAppender();
            memory.ActivateOptions();
            hierarchy.Root.AddAppender(memory);
            hierarchy.Configured = true;
        }

希望这对你有帮助。

谢谢