我有两个控制台应用程序在同一个解决方案中共享常见的app.config和log4.net文件。第一个控制台应用程序调用第二个控制台应用程序,但需要为每个应用程序写入不同的日志文件。两个应用程序共享相同的二进制文件夹。
我的log4.net文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="SchedulerService" type="log4net.Appender.RollingFileAppender">
<file value="c:\\logs\\MailChimp\\Scheduler.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[--------------------------------------------------] " />
<footer value=" " />
<conversionPattern value="%-5level %date [%thread] %logger
%message%newline" />
</layout>
<filter type="log4net.Filter.LoggerMatchFilter">
<acceptOnMatch value="true" />
<!-- set your class name here -->
<LoggerToMatch value="MailChimp.MailingPlan" />
</filter>
<!-- <filter type="log4net.Filter.DenyAllFilter" /> -->
</appender>
<appender name="MailChimp" type="log4net.Appender.RollingFileAppender">
<file value="c:\\logs\\MailChimp\\MailChimpUploader.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[--------------------------------------------------] " />
<footer value=" " />
<conversionPattern value="%-5level %date [%thread] %logger
%message%newline" />
</layout>
<filter type="log4net.Filter.LoggerMatchFilter">
<acceptOnMatch value="true" />
<!-- set your class name here -->
<LoggerToMatch value="MailChimp.MailChimpUploader" />
</filter>
<!--<filter type="log4net.Filter.DenyAllFilter" /> -->
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="ALL" />
<appender-ref ref="SchedulerService" />
<appender-ref ref="MailChimp" />
</root>
</log4net>
只有一个日志文件被写入,显然一个日志记录进程将文件锁定在另一个上,因为Log4Net正在生成以下错误:
log4net:ERROR [RollingFileAppender] ErrorCode: GenericFailure.
Unable to acquire lock on file
c:\logs\MailChimp\MailChimpUploader.txt.
The process cannot access the file
'c:\logs\MailChimp\MailChimpUploader.txt'
because it is being used by another process.
我可以将应用程序合并到一个应用程序中,但我不愿意。对于如何同时从两个应用程序成功登录,我们将不胜感激。
谢谢。
答案 0 :(得分:0)
根据http://logging.apache.org/log4net/release/faq.html,如何让多个进程记录到同一个文件?
<lockingModel type="log4net.Appender.FileAppender+InterProcessLock" />