-1

We have a console application name JetIDR. Where we are using log4net for logging log . THis application are currently creating 2 log file JetIDR-INFO.LOG and JetIDR-Debug.LOG .We want to enhance out application to support flexibility on creating log file as below.

  • Command line parameter should be named as -log
  • Valid parameter for -loglevel are 1 and 2 only
  • When parameter 1 is used with -loglevel JetIDR-INFO.LOG file should get created
  • When parameter 2 is used with -loglevel then JetIDR-INFO.LOG & JetIDR-DEBUG.LOG file should get created

We need to do it in C#.

stuartd
  • 70,509
  • 14
  • 132
  • 163

1 Answers1

1

Your question is effectively "how do I conditionally suppress output to an appender?", where the condition is "If the -loglevel is 1, don't write to the debug logs."

The code would then look like this:

if (logLevel == 1)
{
    // assuming appender name is DebugAppender and it is a FileAppender
    var appender = log4net.LogManager.GetRepository()
                                        .GetAppenders()
                                        .OfType<FileAppender>()
                                        .SingleOrDefault(a => a.Name == "DebugAppender");
    if (appender != null)
    {
        // Disable the appender
        appender.Threshold = Level.Off;
        appender.ActivateOptions();
    }
}

Note however that if the appender is defined in configuration, the log file is created when log4net is configured: this code thus cannot stop the file from being created, but it will suppress logging to the file.

stuartd
  • 70,509
  • 14
  • 132
  • 163