Need server-side login advice using log4net

We use log4net for server-side logging. The problem we are facing is related to 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"> <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 fact is that when several threads write to the log file, log4net sometimes cannot get a lock for the file, because another thread is registered at the same moment. The problem manifests itself in the first case when an IOException is handled by log4net internally.

I'm not sure that we are using log4net correctly in this case and would like some advice on how to improve its configuration.

Thanks.

+4
source share
3 answers

With MinimalLock, you instruct FileAppender to only lock the log file when it writes log messages, and immediately releases the lock on write. This means that other processes will be able to use the lock in the log file between the log flushes.

The fact that your log file is located in the TEMP folder (where many processes do its temporary work) increases the risk that other processes "fake" your log file.

My advice in your scenario:

  • Use RollingFileAppender . This is a more scalable application when it comes to large volumes of magazines.
  • Create a dedicated folder for the log files.
  • Use default ExclusiveLock to improve log performance
+4
source

It is interesting to me. Perhaps a way around this is to use something like the MSMQ appender . Then write some application to write these messages to the file system. There's also an example Async application that might be worth exploring.

+1
source

We use log4net for our multi-threaded applications. It never causes a problem, we don’t have a <lockMode>.

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

0
source

All Articles