Log4net: The process cannot access the file because it is being used by another process

1.4k Views Asked by At

I have an asp.net website that is utilizing log4net. Due to several third party libraries (like NHibernate), there are several appenders across multiple threads. IIS is setup so there should be only one process. I have confirmed this by utilizing the process id in the filename.

Web.config Excerpt:

<appender name="AppAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="Logs\App_[%processid]_.log" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <datePattern value="yyyy-MM-dd" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="false" />
    <preserveLogFileNameExtension value="true" />
    <threshold value="DEBUG" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=" %date %-5level [%property{SessionId}] - %message%newline" />
    </layout>
</appender>

When recycling the app pool in IIS I sometimes get these errors:

log4net:ERROR [RollingFileAppender] ErrorCode: GenericFailure. Unable to acquire lock on file App_[6844]_2014-12-18.log. Access to the path 'App_[6844]_2014-12-18.log' is denied.

My question, is are some of these error inevitable because its multi-threaded, or if configured properly should they never occur.

Additional Info:

  • I'm using log4net 1.2.13 (installed over nuget)
  • Frozen Mountain's WebSync (3.5.1.3), also note that this library directly references log4net 1.2.11, I used a bindingRedirect to 1.2.13
  • NHibernate version 3.1.0.400, it was the first version to not use log4net 1.2.10

**NHibernate can easily be upgraded if we think thats the problem. WebSync would be more difficult.

0

There are 0 best solutions below