tags:

views:

376

answers:

3

Dear ladies and sirs.

We are using log4net for our server side logging. The problem we are facing is with the FileAppender. Here is our log4net section in the app.config file:

  <log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    <appender name="MainAppender" type="log4net.Appender.FileAppender">
      <lockingMode type="log4net.Appender.FileAppender+MinimalLock" />
      <file value="${TMP}\Shunra.Infra.Test.Host.log" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ERROR" />
      <appender-ref ref="MainAppender" />
    </root>
  </log4net>

The thing is that when multiple threads write to the log file, log4net sometimes fails to acquire the lock to the file, because another thread is logging at this same moment. The problem manifests itself in a first chance IOException handled by log4net internally.

I am not sure that we are using log4net correctly in this case and would like to get an advice on how to improve its configuration.

Thanks.

+1  A: 

I wonder this myself. Perhaps a way to get round this is to use something like an MSMQ appender. Then write some kind of app to write these messages to the file system. There's also an Async appender example which might be worth investigating.

RichardOD
Logging to a database using the AdoNetAppender is also a viable option
Peter Lillevold
A: 

We're using log4net for our multithreaded applications. Never causes a problem, we don't have the < lockingMode > section.

Or are you using multiple server applications that access the same log file?

Carra
Nope, just one server with multiple threads in it. Is it OK to remove the lockingMode element? How will the output look like if two threads write to the same file at the same time? Will the output from both threads interleave into a gibrish?
mark
@mark - log4net internally handles serializing log messages into the appender so no, it will not be giberish, each log message is appended to the file on a line-by-line basis
Peter Lillevold
So, I can get rid of the lockingMode element entirely?
mark
+1  A: 

With MinimalLock you instruct the FileAppender to only aquire a lock to the log file when it writes log messages and immediately release the lock when done writing. This means that other processes will have the opportunity to aquire a lock to the log file in between log flushes.

The fact that your log file is located in the TEMP folder (where a lot of processes do their temporary work) increases the risk of other processes "tampering" with your log file.

My advice in your scenario is:

  1. Use RollingFileAppender. This is a more scalable appender when it comes to handling large log amounts.
  2. Create a dedicated folder for your log files.
  3. Use default ExclusiveLock to improve log performance
Peter Lillevold
Agreed the problem is the MinimalLock
csharptest.net
Guys, no one write the log file but the server. However, there are multiple threads writing the same log file. The error indicates that a thread fails to acquire the lock, because another thread is writing it. If I do an exclusive lock, then only one thread will write the log file, ever. That is not wanted.
mark
Then something is amiss. log4net is thread safe so you should be perfectly fine having multiple threads doing logging...
Peter Lillevold