Question

I have two console applications that share common app.config and log4.net files in the same solution. The first console app calls the second console app, but needs to write to a different log file for each application. Both apps share the same binary folder.

My log4.net file looks like this:

<?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="[--------------------------------------------------]&#13;&#10;" />
      <footer value="&#13;&#10;" />
      <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="[--------------------------------------------------]&#13;&#10;" />
      <footer value="&#13;&#10;" />
      <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>

Only one of the log files gets written to and apparently one logging process is locking the file on the other because Log4Net is generating the following error:

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.

I could combine the apps into one application, but I'd rather not. Any suggestions would be appreciated as to how I can successfully log from both apps simultaneously.

Thank you.

No correct solution

OTHER TIPS

According to http://logging.apache.org/log4net/release/faq.html, under the section How do I get multiple process to log to the same file?

<lockingModel type="log4net.Appender.FileAppender+InterProcessLock" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top