Wednesday, February 08, 2006

Using Log4Net's FileAppender in a web garden

Whilst you might not think you're running a web garden (that is: running your website in two or more processes simultaneously on the same machine), it's worth checking in case your server SOE catches you by surprise (hint: this happened to me):

Obviously in this scenario it's not just log4net you've got to watch out for, but anything you're saving on the local filesystem is a candidate for cross-process resource contention. In log4net's case, the RollingFileAppender can block itself (in the other process).

There's a couple of ways round this. You can configure the FileAppender to minimize the amount of time it locks the file (the MinimalLock attribute), but for diagnostic simplicity I prefer to configure each web application instance to log to a different file. Something like this:

<appender name="File" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\temp\MyApp_[%processid].log" />
<datePattern value="yyyyMMdd"/><!--roll the file daily-->
<rollingStyle value="Date"/>
<appendToFile value="true"/>

<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %-5level %logger.%method() - %message [%identity]%newline" />

This way if anything screwy happens in one of the instances (it didn't start alltogether?) it's easier to isolate.

PS: Blogger is so not the right engine to be doing this XML stuff in...


kfra said...

Hi, I am experiencing problems using IIS 6.0 (web garden) and RollingFileAppender configured with <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> directive. It seems that when the daily log rotates, the rotated copy is dramatically down-sized (eg: daily log size before rotation 300k after rotation 1k). Did anyone find out a solution to this problem?

Anonymous said...

I noticed the same. Did anyone found a fix for this?

Anonymous said...

Hi the same problem for me. Any ideas?

Unknown said...

I have the same problem. Can anyone help us?

Popular Posts