Reading from log files

Topics: Logging Application Block
Nov 5, 2012 at 7:24 AM

Hi,

I have an application that writes log lines to files via logging application block, and I am building an application that reads the log files.

My problem is that these two applications can work at the same time, so, the "write to log file" application keeps the log file open, and the "read from log file" application can't open the file to read from it.

Is there any solution for this?

Thanks,

Reut.

Nov 5, 2012 at 6:03 PM
Edited Nov 6, 2012 at 8:52 PM

You can use this code to read a locked file:

string file = @"C:\trace.log";

using(var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using(var sr = new StreamReader(fs))
{
    while(!sr.EndOfStream)
    {
        Console.WriteLine(sr.ReadLine());
    }
}            

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com