[英]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: 这是一个示例方案:
program.exe A
starts and begins to log to A_{currentDate}.log
计划任务program.exe A
启动并开始记录到A_{currentDate}.log
program.exe B
starts while program.exe A
is still running and should start logging to B_{currentDate}.log
计划任务program.exe B
在program.exe A
仍在运行时启动,并应开始记录到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. 当它们都结束时,第二个文件为空,而第一个文件为空,这不是预期的结果,因为我100%确信第二个实例有日志要记录。 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. 鉴于我们为同一程序安排了约20个预定任务,这会导致很多问题,因为我们缺少一些重要的日志。
Here's a snippet of log4net.config
file: 这是log4net.config
文件的一个片段:
<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. 在我看来,您的两个进程都试图同时打开两个文件(附加程序A和附加程序B),因此无论哪个先启动,都会阻止第二个进程运行。 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. 您可能需要为每个唯一的进程设置第二个GlobalContext属性,并将其包含在文件名中,因为听起来两个进程都在同一日期运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.