TraceListener issue

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();

}
 
D

Dmitriy Lapshin [C# / .NET MVP]

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.
 
M

Mullin Yu

i tried Trace.Close();

but, still having error when clicking second tme of the button.

the error is:
Cannot write to a closed TextWriter

therefore, it seems that i can't close it.

thanks!

Dmitriy Lapshin said:
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 said:
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();

}
 
C

Curtis Herrick

I tried out your code, as I'm trying to figure this tracing
functionality out, and it works great for me. I put it in a
button_click event with a tweak here and there out of superstition,
and it seems to work fine. Here's my tweaked version:

TraceSwitch oTS1 = new
TraceSwitch("switch1","switch1");

FileStream oOutputFile = new
FileStream(Application.StartupPath +
@"\switch1.txt",System.IO.FileMode.Append);
TextWriterTraceListener oTL = new
TextWriterTraceListener(oOutputFile,"OUTFILE");

Trace.Listeners.Add(oTL);

Trace.WriteLine("Testing 123");
Trace.WriteLineIf(oTS1.TraceVerbose ,"Verbse Tracing "
+ System.DateTime.Now.ToString());
Trace.WriteLineIf(oTS1.TraceInfo,"InfoTracing " +
System.DateTime.Now.ToString());
Trace.WriteLineIf(oTS1.TraceWarning,"WarningTracing "
+ System.DateTime.Now.ToString());
Trace.WriteLineIf(oTS1.TraceError,"ErrorTracing " +
System.DateTime.Now.ToString());

Trace.Flush();
Trace.Listeners.Remove("OUTFILE");
oOutputFile.Close();
oOutputFile = null;
oTS1 = null;

Hope this is of some use to you.
 
Joined
Mar 13, 2006
Messages
1
Reaction score
0
Yes the above solution will work . Cauz we need to remove the Listener from the Trace once we are done with it , otherwise Trace will keep a reference of earlier listener .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top