File Locking and Rolling Issues

Topics: Logging Application Block
Jan 20, 2011 at 12:27 AM


I'm having an issue with the Logging Application Block provided by the Enterprise Library.  We use Rolling Flat File Trace Listeners to handle logging.  They are configured to roll after they reach 10000 KB in size.  The problem I'm having started with files getting locked, preventing them from being archived.  For example, I might have log files FileA.log, FileA.2011-01-19.1.log, FileA.2011-01-19.2.log, etc.  We have a utility that moves all of the rolled log files to an archive server and then deletes them.  However, the files would sometimes be locked by the application that wrote to them because they had not been restarted, even though they are no longer being used.  I looked for a solution to this and found a couple of posts that say that calling Logger.Writer.Dispose() should release the locks.  Here are some example posts that I saw:

That worked like a charm for releasing the file locks but it introduced a new problem - the files are not being rolled at all and are growing indefinitely, in one case to 2.5 GB.  On top of that it was mentioned in this thread that you should never call Logger.Writer.Dispose().

So my question is what can I do to work around this programmatically?  We have possibly over 100 apps, some of them websites, some windows services, etc., many of which run for days or weeks at a time before being restarted.  I need the rolled log files to be unlocked so that they can be archived.  Is there anything I can do?

Thanks in advance.

Jan 20, 2011 at 9:39 AM

I haven't tried it myself yet the problem you have stated. Though just wondering if the workaround stated in this thread will work for you. To re-iterate the suggested workaround here are the following steps;

1. Do not dispose the LogWriter/Logger.Writer object.

2. Copy the contents of the log file using this code:

FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader(fs);
string log =  reader.ReadToEnd();

3. Write the contents of the file (log variable) to a new file where you wish to copy it.

Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.

Jan 20, 2011 at 6:58 PM

Hi Gino, and thanks for your quick response.

Unfortunately, that doesn't answer my question.  I have no problem reading the contents of the file.  What I need to do is delete the rolled files after they are no longer in use.  I don't need to delete the current log file, just the rolled files.  So, my question remains.  In the application that's writing to the log files, how can I release the locks on the rolled files?  I don't need to do anything with the current log file.  Unfortunately, I'm stuck using Ent Lib 3.1.  The library is heavily used, so the potential problems of upgrading are too much to risk at this time.  Ideally, there is a workaround we could implement in version 3.1.

Jan 21, 2011 at 12:27 AM

I really don't know how to delete a locked file and since you don't have the option to upgrade your EntLib version, what I could recommend is creating a custom rolling flat file trace listener copying the functionality of the latest one from EntLib 5.0.  It has a Max Archived Files property which allows you to specify the number of rolled files that you want to keep so that if it exceeds that number, it'll just delete the old files. 


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.