TextWriterTraceListener Class
Directs tracing or debugging output to a TextWriter or to a Stream, such as Console.Out or FileStream.
For a list of all members of this type, see TextWriterTraceListener Members.
System.Object
System.MarshalByRefObject
System.Diagnostics.TraceListener
System.Diagnostics.TextWriterTraceListener
[Visual Basic] Public Class TextWriterTraceListener Inherits TraceListener [C#] public class TextWriterTraceListener : TraceListener [C++] public __gc class TextWriterTraceListener : public TraceListener [JScript] public class TextWriterTraceListener extends TraceListener
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
The TextWriterTraceListener class provides the Writer property to get or set the text writer that receives the tracing or debugging output.
This class also provide methods to Close the Writer so that it no longer receives tracing or debugging output, to Flush the output buffer for the Writer, and to Write a message to the Writer.
You must enable tracing or debugging to use a switch. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.
- To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or you can add #define DEBUG to the top of your file. In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.
- To enable tracing using in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or add #define TRACE to the top of your file. In Visual Basic, add the /d:TRACE=True flag to the compiler command line.
To set the level of your listener, edit the configuration file that corresponds to the name of your application. Within this file, you can add a listener, set its type and set its parameter, remove a listener, or clear all the listeners previously set by the application. The configuration file should be formatted like the following example.
For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.
<configuration>
<system.diagnostics>
<switches>
<add name="MagicTraceSwitch" value="3" />
</switches>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\myListener.log" />
<!-- You must supply a valid fully qualified assembly name here. -->
<remove type="Assembly text name, Version, Culture, PublicKeyToken"/>
</listeners>
</trace>
</system.diagnostics>
</configuration> Example
[Visual Basic, C#, C++] The following example implements two instances of the TextWriterTraceListener class. The first instance, myTextListener, uses a StreamWriter called myOutputWriter to write to a file named TestFile.txt. The second instance, myWriter, outputs to the console screen.
[Visual Basic, C#, C++] First the example creates a file for output. Then it creates the StreamWriter for the first text writer, assigns it the output file, and adds it to the Listeners. Next it creates the second text writer, which it also add to the Listeners. Then, the code outputs one line of text to the file and the console screen, and two lines to the console screen. Finally, the example flushes the output buffer and closes the buffer.
[Visual Basic, C#, C++] After running this sample, you can open the TestFile.txt file to see the output.
[Visual Basic] Public Class Sample Public Shared Sub Main() ' Create a file for output named TestFile.txt. Dim myFile As Stream = File.Create("TestFile.txt") ' Create a new text writer using the output stream, and add it to ' the trace listeners. Dim myTextListener As New TextWriterTraceListener(myFile) Trace.Listeners.Add(myTextListener) ' Create a text writer that writes to the console screen, and add ' it to the trace listeners Dim myWriter As New TextWriterTraceListener(System.Console.Out) Trace.Listeners.Add(myWriter) ' Write output to the file and to the console screen. Trace.Write("Test output ") ' Write only to the console screen. myWriter.WriteLine("Write only to the console screen.") ' Flush and close the output. Trace.Flush() myWriter.Flush() myWriter.Close() System.Environment.ExitCode = 0 End Sub End Class [C#] public class Sample { public static int Main(string[] args) { // Create a file for output named TestFile.txt. Stream myFile = File.Create("TestFile.txt"); /* Create a new text writer using the output stream, and add it to * the trace listeners. */ TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile); Trace.Listeners.Add(myTextListener); /* Create a text writer that writes to the console screen, and add * it to the trace listeners */ TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out); Trace.Listeners.Add(myWriter); // Write output to the file and to the console screen. Trace.Write("Test output "); // Write only to the console screen. myWriter.WriteLine("Write only to the console screen."); // Flush and close the output. Trace.Flush(); myWriter.Flush(); myWriter.Close(); return 0; } } [C++] int main() { // Create a file for output named TestFile.txt. Stream* myFile = File::Create(S"TestFile.txt"); /* Create a new text writer using the output stream, and add it to * the trace listeners. */ TextWriterTraceListener* myTextListener = new TextWriterTraceListener(myFile); Trace::Listeners->Add(myTextListener); /* Create a text writer that writes to the console screen, and add * it to the trace listeners */ TextWriterTraceListener* myWriter = new TextWriterTraceListener(System::Console::Out); Trace::Listeners->Add(myWriter); // Write output to the file and to the console screen. Trace::Write(S"Test output "); // Write only to the console screen. myWriter->WriteLine(S"Write only to the console screen."); // Flush and close the output. Trace::Flush(); myWriter->Flush(); myWriter->Close(); return 0; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Diagnostics
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System (in System.dll)
See Also
TextWriterTraceListener Members | System.Diagnostics Namespace | TraceListener | DefaultTraceListener | EventLogTraceListener | Stream | TextWriter | Debug | Trace