Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net GenericFailure. Unable to acquire lock on file

Tags:

c#

config

log4net

I am trying to configure log4net for the first time, I'm certain that I have everything configured properly, however, after getting empty log files I turned on the log4net debugger. I am now continually seeing the following error:

log4net:ERROR [RollingFileAppender] ErrorCode: GenericFailure. Unable to acquire lock on 
file "file path\file name" The process cannot access the file "file path\file name" because 
it is being used by another process.

I currently have log4net configured through my Web.config file thusly:

<log4net debug="true">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="Logs\\TransferPicturesApplicationLog.txt"/>
  <appendToFile value="true"/>
  <rollingStyle value="Size"/>
  <maxSizeRollBackups value="5"/>
  <maximumFileSize value="10MB"/>
  <staticLogFileName value="true"/>
  <filter type="log4net.Filter.LevelRangeFilter">
    <param name="LevelMin" value="ERROR"/>
    <param name="LevelMax" value="DEBUG"/>
  </filter>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline"/>
  </layout>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<root>
  <level value="INFO"/>
  <appender-ref ref="RollingFileAppender"/>
</root>
<logger>
  <level value="DEBUG"/>
  <appender-ref ref="RollingFileAppender"/>
</logger>
</log4net>

Help!!!

like image 820
Homer2029 Avatar asked Sep 10 '12 14:09

Homer2029


2 Answers

My guess would be that you have multiple copies of the appender across different assemblies trying to access this path, so they all attempt to take hold of the appropriate log file at the same time. However, to be sure, I would recommend you use Unlocker to make sure your file isn't being held by something else you don't expect. If you are having multiple instances of this appender trying to open the same file, your problem would be that you are not using the appender correctly; if the appender is being used across assemblies then the two invocations of it will not play nicely with one another, leading to an already in use error like this. If this is the case, refactoring is likely your only option.

like image 84
tmesser Avatar answered Oct 23 '22 21:10

tmesser


I also got this error because I specified the name of a directory as opposed to a file in the appender, the error message is cryptic:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <file value="C:\DirectoryNameHere\MyBad" />
like image 7
PeteN Avatar answered Oct 23 '22 21:10

PeteN