System 2.0.0.0 4.0.0.0 System.Diagnostics.TextWriterTraceListener

Use the class to write trace and debugging messages to the console. You can initialize a object to write trace messages to the stream or to the stream.

When trace and debugging output is enabled, the messages are written to the specified stream, which is similar to the way messages are written with the or methods. In a console application, the output and error streams write messages to the existing console window, or you can redirect the streams to write to a instance.

If the console does not exist, as in a Windows-based application, messages written to the console are not displayed.

Add a object to the appropriate collection if you want messages written through , , or to be written to the console. In addition, you can write messages directly to the console using the or methods.

The and classes share the same collection, accessed through their respective Listeners properties. If you add a object to the collection using one of these classes, the other class automatically uses the same listener.

Most compilers enable trace and debug output through conditional compilation flags. If you do not enable tracing or debugging, the messages written through the and classes are effectively ignored. The syntax to enable trace and debug output 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 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.

You can add a object to the collection in your code, or you can add a object to the collection through the application configuration file. Add the object in your code to write messages for a specific code section or execution path. Add the object in your application configuration file to direct all trace and debug messages to the console while the application executes.

To write trace and debug messages to the console for a specific section of code, initialize a object and add it to the collection. Instrument the section of code that contains messages using the or classes. At the end of the code section, remove the object from the collection, and call the method on the .

To direct all trace and debug messages to the console while the application executes, add a object to the application configuration file. Edit the configuration file that corresponds to the name of your application, or the app.config file in a vsprvslong project. In this file, insert an element for a .

The following example adds a object named configConsoleListener to the collection.

`<configuration> <system.diagnostics> <trace autoflush="false" indentsize="4"> <listeners> <add name="configConsoleListener" type="System.Diagnostics.ConsoleTraceListener" /> </listeners> </trace> </system.diagnostics> </configuration>`

For details about adding trace listeners in the application configuration file, see <listeners> Element.

Directs tracing or debugging output to either the standard output or the standard error stream.

- Constructor
  2.0.0.0
  4.0.0.0

  This constructor initializes a object to write messages to the stream. Its property is initialized to an empty string ("").

  Initializes a new instance of the class with trace output written to the standard output stream.

- Constructor
  2.0.0.0
  4.0.0.0

  This constructor initializes a object to write messages to either the or the stream. Its property is initialized to an empty string ("").

  Initializes a new instance of the class with an option to write trace output to the standard output stream or the standard error stream.

  true to write tracing and debugging output to the standard error stream; false to write tracing and debugging output to the standard output stream.