Log4net File gets locked even though browser site is closed

2k Views Asked by At

I am using VS 2013 and Log4Net to log data for a .Net application. I can create logs. But when i try to delete or move the log file , it says

The file is open in another process... 

This message i get even though the browser is closed. I am only able to edit/cut/ rename.. the log file when i close the Visual studio IDE tool. How can i solve this problem. I want the ability to delete / edit the file whenever i want. Here is the code for log4net used in web.config

 <configSections>
 <section name="log4net" 
 type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 </configSections>

 <log4net>
   <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
   <file value="c:\logs\jwhXMLDev.log" />
   <appendToFile value="true" />
   <!-- <layout type="log4net.Layout.XmlLayout"/>-->
   <layout type="log4net.Layout.XmlLayout" />
   </appender>
   <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
   <onlyFixPartialEventData value="true" />
   </appender>
<root>
  <level value="ALL" />
  <appender-ref ref="RollingFile" />
</root>
</log4net>
1

There are 1 best solutions below

2
On BEST ANSWER

Regarding your comment about closing the browser

It's not the browser that opens/locks/manipulates the files but the Web Server.

The browser sends a request to the web server which replies with a response but in the meantime there are no links between the two outside of these requests/responses exchanges. If you close the browser after a response has been received, the server does not know it. It simply awaits for more possible requests by the browser, it can't know it has been closed.

Your web server is probably IIS Express or Local IIS. It's this Web Server which is the process locking the file.

How to release the file right now

If you are using IIS Express, it is started/shut down when you start the application in Visual Studio. When you stop debugging it should release the file.

If you are using a local or remote IIS and you can't get it to release a file, recycling the application pool or restarting IIS will do the trick.

Preventing this from happening

Log4Net keeps a lock on the file but you can modify it's behaviour by adding the following line to your <appender element:

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

Here's your config again with the added line:

<configSections>
 <section name="log4net" 
 type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 </configSections>

 <log4net>
   <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
   <file value="c:\logs\jwhXMLDev.log" />
   <appendToFile value="true" />
   <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
   <!-- <layout type="log4net.Layout.XmlLayout"/>-->
   <layout type="log4net.Layout.XmlLayout" />
   </appender>
   <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
   <onlyFixPartialEventData value="true" />
   </appender>
<root>
  <level value="ALL" />
  <appender-ref ref="RollingFile" />
</root>
</log4net>