Logging to an individual log file for each individual thread

前端 未结 3 1320
灰色年华
灰色年华 2020-12-03 15:06

I have a service application that on startup reads an XML file and starts a thread for each entry in the XML file. Each thread creates an instance of a worker class which re

3条回答
  •  一向
    一向 (楼主)
    2020-12-03 15:40

    I think I have worked out the issue. The steps follow:

    • Create an an individual ILoggerRepository object (loggerRepository in this example) on each thread.
    • Set the ThreadContexts property for the log file name.
    • Use the XmlConfiguratior to configure the repository.
    • Use the LogManager to Get the named logger (in the XML configuration file) using the named LoggerRepository for that thread.

    In return I get a new configured logger pointing to the respective file for that thread.

    The XML configuration is the same as it was originally and shown here for completeness:

    
    
          
        

    The code to create the loggers is below. Each time this code is run it is run in its own thread.

    ILoggerRepository loggerRepository = LogManager.CreateRepository(logFileName + "Repository");
    ThreadContext.Properties["LogName"] = logFileName;
    log4net.Config.XmlConfigurator.Configure(loggerRepository);
    ILog logger = LogManager.GetLogger(logFileName + "Repository", "ProductionLogger");
    

    This seems to work with no issues so far. I will be moving forward with this solution for the moment but I will update this post if I find out anything else.

提交回复
热议问题