M
Mullin Yu
I create a component to write the error log as follow, but it seems
sometimes locked by other processes, and pop up the I/O exception.
How can we handling the concurrency issue at file level? Retry accessing the
file if catch error => like waiting and polling.
Thanks!
private void LogError2File(string errorMessage, string LogFilePath)
{
FileInfo oFileInfo = new FileInfo(LogFilePath);
DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);
if(!oDirInfo.Exists)
oDirInfo.Create();
if(!oFileInfo.Exists)
oFileInfo.Create();
StreamWriter w = File.AppendText(LogFilePath);
w.Write("\r\nLog Entry : ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", errorMessage);
w.WriteLine
("----------------------------------------------------------------");
// Update the underlying file.
w.Flush();
// Close
w.Close();
}
sometimes locked by other processes, and pop up the I/O exception.
How can we handling the concurrency issue at file level? Retry accessing the
file if catch error => like waiting and polling.
Thanks!
private void LogError2File(string errorMessage, string LogFilePath)
{
FileInfo oFileInfo = new FileInfo(LogFilePath);
DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);
if(!oDirInfo.Exists)
oDirInfo.Create();
if(!oFileInfo.Exists)
oFileInfo.Create();
StreamWriter w = File.AppendText(LogFilePath);
w.Write("\r\nLog Entry : ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", errorMessage);
w.WriteLine
("----------------------------------------------------------------");
// Update the underlying file.
w.Flush();
// Close
w.Close();
}