File I/O Exception: The Process Cannot Open The File...

J

Jeremy S.

I have two applications running on the same server.

App 1 is an asp.net Web application that is installed multiple times on the
same server (one installation per Web site hosted on the server). Each
installation writes runtime exception information to an XML log file; one
exception per file (so 5 runtime exceptions would result in 5 exception log
files).

App 2 is a Windows Service that monitors each installation App 1-
specifically looking for new exception log files. It uses a separate
instance of the FileSystemWatcher to monitor each installation of App 1 -
watching for new exception log files. When a new log file is found, App2
then attempts to open it, pulling out certain important summary information,
and then sends e-mail messages to tech support staff (i.e., to show up on a
production support pager). There is more logic to it, but that's basically
what's going on here.

The problem is that App2 periodically cannot open a newly detected log
file - with the exception, "The process cannot open the file, [path here],
because it is being used by another process."

This exception is encountered rather infrequently (most of the time the
newly detected log file is opened successfully).

Question: What can I do to read the file even when this exception is
encountered? I suspect the Windows Service app is detecting the file before
App1 is finished writing to it... is there any way to have the Service wait
a bit and then try again? If so, how? Or, is there some better way to deal
with this?

FWIW: here's how App1 creates each log file:
MyWriter = new XmlTextWriter(xmlFilePath, null);
MyWriter.Formatting = Formatting.Indented;
MyWriter.WriteStartDocument();
.... <<write exception details here via MyWriter methods>>
MyWriter.Flush();
MyWriter.Close();

Then, App2 (Windows Service) opens the newly detected log file like this:
System.Xml.XmlTextReader rdr = new
System.Xml.XmlTextReader(pathToExceptionLog);
while (rdr.Read()) {
.... <<extract summary data here>>
}
rdr.Close();


Thanks!
 
G

Guest

Try this in the writers:
FileStream f = new FileStream("path", FileMode.Open, FileAccess.Write,
FileShare.Read);
XmlTextWriter xt = new XmlTextWriter(f, null);

FileShare will allow other processes to read from the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top