Re: TraceListener issue

From: Dmitriy Lapshin [C# / .NET MVP] (x-code_at_no-spam-please.hotpop.com)
Date: 02/20/04


Date: Fri, 20 Feb 2004 13:53:53 +0200

Hi,

You can try closing the stream by using the Trace's Close method, not the
FileStream's one.
You don't need an explicit call to Flush then.

-- 
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"Mullin Yu" <mullin_yu@ctil.com> wrote in message
news:ujXKuh59DHA.2480@TK2MSFTNGP12.phx.gbl...
> 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();
>
> }
>
>