You could try opening and closing the file within the while loop.
That way while your app is sleeping it doesn't have a hold on the file.
You'll need to check for BaseStream.Length being smaller than your stored
lastMaxOffset when the other app renames the file.
C#/PsuedoCode
long lastMaxOffset;
while (true)
{
System.Threading.Thread.Sleep(100);
using (StreamReader reader = new StreamReader(new FileStream(fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
//if the file size has not changed, idle
if (reader.BaseStream.Length == lastMaxOffset)
continue;
//Handle log file being reset
if (reader.BaseStream.Length < lastMaxOffset)
lastMaxOffset = 0;
//seek to the last max offset
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);
//read out of the file until the EOF
string line;
while ((line = reader.ReadLine()) != null)
//Store Lines
//update the last max offset
lastMaxOffset = reader.BaseStream.Position;
}
//Process Lines
}
You're still going to have the risk of having the file open when the owner app
renames the file, but by not having the file open when you sleep or process the
lines you've read, it's minimized.
Graeme Taylor <[EMAIL PROTECTED]> wrote: Hi,
I am writing an application that will watch log files for certain text patterns
appearing (e.g. errors / warnings). So far, I have created an application that
picks up new lines and will check them using RegEx (based on CodeProject
article http://www.codeproject.com/cs/files/tail.asp).
However, most applications that create the logs will try to rename the log file
when it reaches a certain size, at the moment, my Log Watching application is
preventing this rename taking place.
Would be grateful for any tips on how to get round this.
Thanks in advance for any assistance,
Graeme
___________________________________________________________
Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for
your free account today
http://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winter07.html
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com