M
Mullin Yu
i have a problem with TraceListener. when i close the FileStream, i will
have the error:
"Cannot access a closed file"
but, if i remarks the statement => no closing the FileStream, i will have
another error:
"The process cannot access the file "C:\MyAppsTraceLog.log" because it is
being used by another process."
Remind that it's ok when i click the button once. the problem arises when
clicking at the second time.
my coding is:
private void button1_Click(object sender, System.EventArgs e)
{
// Create a TextWriteTraceListener to
// capture all Debug and Trace messages
// Define our tracing log file
System.IO.FileStream traceLog = new
System.IO.FileStream(@"C:\MyAppsTraceLog.log",
System.IO.FileMode.OpenOrCreate);
// Instantiate a new TextWriterTraceListener and specify the output location
TextWriterTraceListener traceListener = new
TextWriterTraceListener(traceLog);
// Add Listener to the Listeners collection
Trace.Listeners.Add(traceListener);
// Send our tracing messages
Trace.WriteLine("Trace Message 1");
Debug.WriteLine("You are here.");
Trace.WriteLine("Trace Message 2");
// Flush the listners buffer and close the output
Trace.Flush();
// if remark => file is accessed by another process
// traceLog.Close()
traceLog.Close();
}
have the error:
"Cannot access a closed file"
but, if i remarks the statement => no closing the FileStream, i will have
another error:
"The process cannot access the file "C:\MyAppsTraceLog.log" because it is
being used by another process."
Remind that it's ok when i click the button once. the problem arises when
clicking at the second time.
my coding is:
private void button1_Click(object sender, System.EventArgs e)
{
// Create a TextWriteTraceListener to
// capture all Debug and Trace messages
// Define our tracing log file
System.IO.FileStream traceLog = new
System.IO.FileStream(@"C:\MyAppsTraceLog.log",
System.IO.FileMode.OpenOrCreate);
// Instantiate a new TextWriterTraceListener and specify the output location
TextWriterTraceListener traceListener = new
TextWriterTraceListener(traceLog);
// Add Listener to the Listeners collection
Trace.Listeners.Add(traceListener);
// Send our tracing messages
Trace.WriteLine("Trace Message 1");
Debug.WriteLine("You are here.");
Trace.WriteLine("Trace Message 2");
// Flush the listners buffer and close the output
Trace.Flush();
// if remark => file is accessed by another process
// traceLog.Close()
traceLog.Close();
}