M
Mullin Yu
is the following capable of restricting more than one program writing error
log to file? in the past, i got error stating the log file is currently
opened by another process.
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);
//FileStream fs = new FileStream(oFileInfo.FullName, FileMode.Open,
FileAccess.Write,
// System.IO.FileShare.ReadWrite);
lock(oFileInfo)
{
FileStream fs = new FileStream(oFileInfo.FullName, FileMode.Open,
FileAccess.Write,
System.IO.FileShare.Read);
StreamWriter w = new StreamWriter(fs);
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();
}
}
log to file? in the past, i got error stating the log file is currently
opened by another process.
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);
//FileStream fs = new FileStream(oFileInfo.FullName, FileMode.Open,
FileAccess.Write,
// System.IO.FileShare.ReadWrite);
lock(oFileInfo)
{
FileStream fs = new FileStream(oFileInfo.FullName, FileMode.Open,
FileAccess.Write,
System.IO.FileShare.Read);
StreamWriter w = new StreamWriter(fs);
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();
}
}