Jujjizzu Jujjizzu - 1 month ago 10
C# Question

log4net set logging of two files with a different level logging from c# code

This is my problem, i want to log in 2 different files (one for error and one for ordinary information) by c# application and log4net, i found the configuration in xml, but i want to do it from code, this is the source that i found for the setting the logging of one file:

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;
}


in another class for the logging:

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


and

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


the code works.

now, i did this change to get 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: both log files are written with the two messages.

Please kindly suggest, many thanks!

Answer

The issue is you have appended both appenders to the "root" logger. If you want to have two loggers then you must append them to specific loggers.

There I have commented the hierarchy.Root.AddAppender(errorRoller); and hierarchy.Root.AddAppender(infoRoller); since it is no longer required as appenders are appended to relevant loggers instead of the root.

Find the code below. There I have passed log and elog as parameters to the method and append the relevant appenders respectively.

   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);
    }

Invoking the method

    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");