Home »

Can I redirect tracing to a file?

Question ListCategory: ASP.NETCan I redirect tracing to a file?
alisataylore190 author asked 3 years ago
1 Answers
jully882 author answered 3 years ago

Yes. The Debug and Trace classes both have a Listeners property, which is acollection of sinks that receive the tracing that you send via Debug.WriteLine
and Trace.WriteLine respectively. By default the Listeners collection contains
a single sink, which is an instance of the DefaultTraceListener class. This
sends output to the Win32 OutputDebugString() function and also the
System.Diagnostics.Debugger.Log() method. This is useful when debugging,
but if you’re trying to trace a problem at a customer site, redirecting the
output to a file is more appropriate. Fortunately, the TextWriterTraceListener
class is provided for this purpose.
Here’s how to use the TextWriterTraceListener class to redirect Trace output
to a file:
FileStream fs = new FileStream( @”c:log.txt”, FileMode.Create, FileAccess.Write );
Trace.Listeners.Add( new TextWriterTraceListener( fs ) );
Satish Marwat Dot Net Web Resources satishcm@gmail.com 40 Page
Trace.WriteLine( @”This will be writen to c:log.txt!” );
Note the use of Trace.Listeners.Clear() to remove the default listener. If you
don’t do this, the output will go to the file and OutputDebugString().
Typically this is not what you want, because OutputDebugString() imposes a
big performance hit.

Please login or Register to Submit Answer