K
Kris Erickson
Hi,
Have run into a problem with TextWriter.Synchronized where we are
logging errors. The call get the Textwriter looks like this:
return TextWriter.Synchronized(File.AppendText(filename));
Where filename is the name of the log file and there are no other
references to the filename within this or any other project. The error
occurs when no-one is on the machine, so no one is looking at the error
log. And we get this error:
The process cannot access the file
'C:\\error_logs\\ErrorLog_2007-03-30.txt' because it is being used by
another process. I assumed that threadsafe meant not only operations on
the object where threadsafe, but if access to that file was limited to
that synchronized object that there would be no kind of file contention
issues (let me state again that the only access to the logs is through
writing to the LogError function. Is there any way around this error?
The complete function is:
public static void LogError(string logMessage)
{
TextWriter fErrorLog = null;
try
{
fErrorLog =
TextWriter.Synchronized(File.AppendText(LogFilename));
// Create string to write
logMessage = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
": " + logMessage;
// Write string and close file
fErrorLog.WriteLine(logMessage);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print(ex.ToString());
}
finally
{
if (fErrorLog != null)
{
fErrorLog.Close();
}
}
}
Have run into a problem with TextWriter.Synchronized where we are
logging errors. The call get the Textwriter looks like this:
return TextWriter.Synchronized(File.AppendText(filename));
Where filename is the name of the log file and there are no other
references to the filename within this or any other project. The error
occurs when no-one is on the machine, so no one is looking at the error
log. And we get this error:
The process cannot access the file
'C:\\error_logs\\ErrorLog_2007-03-30.txt' because it is being used by
another process. I assumed that threadsafe meant not only operations on
the object where threadsafe, but if access to that file was limited to
that synchronized object that there would be no kind of file contention
issues (let me state again that the only access to the logs is through
writing to the LogError function. Is there any way around this error?
The complete function is:
public static void LogError(string logMessage)
{
TextWriter fErrorLog = null;
try
{
fErrorLog =
TextWriter.Synchronized(File.AppendText(LogFilename));
// Create string to write
logMessage = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
": " + logMessage;
// Write string and close file
fErrorLog.WriteLine(logMessage);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print(ex.ToString());
}
finally
{
if (fErrorLog != null)
{
fErrorLog.Close();
}
}
}