RollingFlatFileTraceListener - log file is locked

Topics: Logging Application Block
Oct 29, 2013 at 4:00 PM
Hello,

I found some posts that discuss about my problem but I didn't found solution. I have problem with RollingFlatFileTraceListener because this listener keeps log file locked but we need to have possibility to read log file any time. Our application use superadmin role to be able to write to log file but some users that wants to read log file dont't have superadmin role. We need to find some solution how to keep log file unlocked if no log is written.

Thank you for your solution.
Oct 30, 2013 at 5:05 AM
The out of the box trace listeners keep the file locked -- there is no way around that. You should be able to read through locks using code such as the following:
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(fs))
{
    while (!sr.EndOfStream)
    {
        Console.WriteLine(sr.ReadLine());
    }
}
Are you sure that locking is the problem? The discussion of superadmin role makes it sounds like you may have a permission problem? You would need to ensure that the user has permission to read the file. This could be done using permissions on the parent folder that should be inherited by any log files created.

You could always develop a custom trace listener that exhibits the exact behavior you wish (but I don't think it should be necessary based on what you've posted).

If that doesn't help, then could you give more specifics about the problem you are trying to solve and the issues you are seeing.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to