Renaming log file that is being read

  • Thread starter Thread starter Mike Blake-Knox
  • Start date Start date
M

Mike Blake-Knox

I have a server using the .NET Framework 1.0 that writes logs onto the
end of a text file. When the log file becomes full, it renames the log
file then creates a new file and writes logs into that file until it
too becomes full.

It is often useful to watch the text being added to the file and
display it in a window (similar to the Unix "tail -f" command). I have
found that when the server log file is opened for reading by this
viewer program, the server cannot rename the file as it receives an
error stating the file is in use by another process. It sounds quite
reasonable for this to be an error as the reading program may receive
an inconsistent view of the file. In this case, I can adjust the
viewer to produce imperfect but usable output and would like to find a
way to avoid interference with the server's activities.

The log fle is accessed across a LAN. The viewer open a FileStream,
reads to the current end of the log file then sleeps for a while and
tries to read again. I have found that I can reduce the frequency of
the problem having the viewer close the file while it sleeps.
Unfortunately, this doesn't solve the problem as it only reduces the
chance that the viewer will have the file open when the server wants to
rename it.

I can add some tolerance of this situation to the server but this might
either reduce the severity of the problem or shift the problem to
delaying the server.

Is there any mechanism to read from the file without affecting the
server? It sounds strange that in this scenario, the reading program is
interfering with the writer rather than the other way around.

Thanks

Mike
 
If the file isn't too big, you might just avoid the whole issue by
copying the log file to another file with a different name and then
doing whatever you like with that file.
 
If the file isn't too big, you might just avoid the whole issue by
copying the log file to another file with a different name and then
doing whatever you like with that file.

Thanks but Murphy's Law probably guarantees that the server will try to
roll over to a new file just as the file is being copied.

Mike
 
Back
Top