0

So I am adding logging functionality to an application using log4net. I have created a class to setup logging since I will need to instantiate multiple loggers. Every time I need to create a new logging instance I intent to invoke this method and pass it the necessary parameters.

public class Logging
{
    /// <summary>
    /// Sets up a new logging instance
    /// </summary>
    public void Setup(string logName, log4net.Core.Level logLevel)
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

        PatternLayout patternLayout = new PatternLayout
        {
            ConversionPattern = "%date [%thread] %-5level - %message%newline"
        };
        patternLayout.ActivateOptions();

        RollingFileAppender roller = new RollingFileAppender
        {
            Name = logName,
            AppendToFile = true,
            File = "path\\to\\log\\ " + logName + ".txt",
            Layout = patternLayout,
            MaxSizeRollBackups = 5,
            MaximumFileSize = "1GB",
            RollingStyle = RollingFileAppender.RollingMode.Size,
            StaticLogFileName = true,
            Threshold = logLevel                   
        };
        roller.ActivateOptions();
        hierarchy.Root.AddAppender(roller);

        //MemoryAppender memory = new MemoryAppender();
        //memory.ActivateOptions();
        //hierarchy.Root.AddAppender(memory);
        hierarchy.Name = logName;
        hierarchy.Root.Level = Level.Info;
        hierarchy.Configured = true;


    }
}

Using this code I can initialize and add new loggers to the collection like so:

        //in a certain context
        //loglevel is an assigned log level
        var fileName = "Client Execution Log";
        var logging = new Logging();
        logging.Setup(fileName, logLevel);
        var executionLog = log4net.LogManager.GetLogger(fileName);

         //in another context
         //loglevel is an assigned log level
        var fileName = "Server Response Log";
        var logging = new Logging();
        logging.Setup(fileName, logLevel);
        var serverLog = log4net.LogManager.GetLogger(fileName);

The problem is any time I call the method to write a log serverLog.info("server responded 404") it ends up writing to all files rather that the file that was passed in when Setup was called. How do I properly set up logging so that each logging instance only writes to its own file that was passed to the setup method?

Jason Bayldon
  • 1,296
  • 6
  • 24
  • 41
  • I am not sure, but I guess you are just ending up with the root logger which will write to all appenders in the tree. You'd need to also configure loggers that only write to one specific appender. I really recommend using a config file over coded configuration. I am sure it would be much clearer, then. – Fildor Aug 15 '18 at 13:47
  • Also, you can switch on internal logging to shed some light on why it logs where it does. https://stackoverflow.com/a/756241/982149 – Fildor Aug 15 '18 at 13:48
  • @Fildor The configuration is used to set the code, so it should not be any different. I guess I can try to set a configuration to see how the classes look... – Jason Bayldon Aug 15 '18 at 15:43

0 Answers0