简体   繁体   中英

Multiple instances of same process logging on different files

I'm trying to log to multiple files with multiple instances of the same scheduled process, and the target file is chosen based on runtime parameters.

The problem here is that, while one instance is running, no other one is able to log, producing just empty files.

Here's a sample scenario:

  • The schedlued task program.exe A starts and begins to log to A_{currentDate}.log
  • The schedlued task program.exe B starts while program.exe A is still running and should start logging to B_{currentDate}.log

When they both end, the second file is empty while the first is not, and this is not the expected result as I'm 100% sure that the second instance has something to log. Basically it looks like that the first instance to run prevents all the other ones from logging, being the only one that can do it.

Given that we have about 20 scheduled tasks for the same program, this is causing a lot of issues as we're missing some important logs.

Here's a snippet of log4net.config file:

<appender name="A_appender" type="log4net.Appender.FileAppender">
    <file type="log4net.Util.PatternString" value="A_%property{Date}.log" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <param name="AppendToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
    </layout>
</appender>

<appender name="B_appender" type="log4net.Appender.FileAppender">
    <file type="log4net.Util.PatternString" value="B_%property{Date}.log" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <param name="AppendToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
    </layout>
</appender>

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
    <layout type="log4net.Layout.PatternLayout">        
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
    </layout>
</appender>

...

<logger name="A.Logger">
    <appender-ref ref="A_appender" />
    <level value="DEBUG" />
</logger>

<logger name="B.Logger">
    <appender-ref ref="B_appender" />
    <level value="DEBUG" />
</logger>

...

<root>
    <level value="ALL" />
    <appender-ref ref="ConsoleAppender" />
</root>

and here's the initialization code for each instance:

protected Module(string logName)
{
    ThreadContext.Properties["Date"] = GlobalContext.Properties["Date"] = DateTime.Now.ToString("yyyyMMdd");            
    XmlConfigurator.Configure(new System.IO.FileInfo(properties.get("log4net.config.file")));
    log = LogManager.GetLogger(logName);
    ...
}

Can someone help me with that?

The way it looks to me is that both of your processes are trying to hold both files open (both appender A and appender B) so whichever one starts first blocks the second process from running. You may only be grabbing a reference to one or the other appender to use, but it is creating both of them even though it is only logging to whichever one you grab.

You will likely need to set a second GlobalContext property for each process that is unique and include that in the filename, since it sounds like both processes run on the same date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM