简体   繁体   English

同一进程的多个实例记录在不同的文件上

[英]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 计划任务program.exe A启动并开始记录到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 计划任务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.

相关问题 使用不同的日志文件登录同一个应用程序 - logging in the same application with different log files 同一订户在不同计算机上的多个实例 - Multiple instances of the same subscriber on different machines 具有相同步骤的Specflow Feature文件导致多个浏览器实例启动 - Specflow Feature files with same steps causing multiple browser instances to launch 加载具有不同版本的同一Assembly并实例化多个.xaml实例 - Loading same Assembly with different versions and instnatiating multiple .xaml instances 在Moq中创建具有不同行为的相同类型的多个实例(使用Autofac) - Create multiple instances of the same type with different behaviour in Moq (with Autofac) 如何在MSI安装程序上部署具有不同名称的同一Web服务的多个实例 - How to deploy multiple instances of same webservice with different names on MSI installer 创建具有不同配置的相同依赖项的多个实例 - Create multiple instances of same dependency w/ different configuration 我们可以注入具有不同变量的相同 class 的多个实例吗? - Can we inject multiple instances of the same class with different variables? 相同接口的 StructureMap,但具有不同构造函数参数的多个实例 - StructureMap for same interface but with multiple instances for different constructor parameters 同一片段的多个实例? - Multiple instances of the same fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM