System.Diagnostics.ConsoleTraceListener Class

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

See Also: ConsoleTraceListener Members

Syntax

public class ConsoleTraceListener : TextWriterTraceListener

Remarks

Use the System.Diagnostics.ConsoleTraceListener class to write trace and debugging messages to the console. You can initialize a System.Diagnostics.ConsoleTraceListener object to write trace messages to the Console.Out stream or to the Console.Error stream.

When trace and debugging output is enabled, the System.Diagnostics.ConsoleTraceListener messages are written to the specified Console stream, which is similar to the way messages are written with the erload:System.Console.Write or erload:System.Console.WriteLine methods. In a console application, the Console output and error streams write messages to the existing console window, or you can redirect the streams to write to a System.IO.TextWriter instance.

Note:

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

Add a System.Diagnostics.ConsoleTraceListener object to the appropriate Trace.Listeners collection if you want messages written through System.Diagnostics.Trace, System.Diagnostics.TraceSource, or System.Diagnostics.Debug to be written to the console. In addition, you can write messages directly to the console using the erload:System.Diagnostics.Trace.Write or erload:System.Diagnostics.Trace.WriteLine methods.

Note:

The System.Diagnostics.Debug and System.Diagnostics.Trace classes share the same System.Diagnostics.TraceListenerCollection collection, accessed through their respective Listeners properties. If you add a System.Diagnostics.ConsoleTraceListener 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 System.Diagnostics.Debug and System.Diagnostics.Trace 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.

You can add a System.Diagnostics.ConsoleTraceListener object to the Trace.Listeners collection in your code, or you can add a System.Diagnostics.ConsoleTraceListener object to the Trace.Listeners collection through the application configuration file. Add the System.Diagnostics.ConsoleTraceListener object in your code to write messages for a specific code section or execution path. Add the System.Diagnostics.ConsoleTraceListener 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 System.Diagnostics.ConsoleTraceListener object and add it to the Trace.Listeners collection. Instrument the section of code that contains messages using the System.Diagnostics.Trace or System.Diagnostics.Debug classes. At the end of the code section, remove the System.Diagnostics.ConsoleTraceListener object from the Trace.Listeners collection, and call the TextWriterTraceListener.Close method on the System.Diagnostics.ConsoleTraceListener.

To direct all trace and debug messages to the console while the application executes, add a System.Diagnostics.ConsoleTraceListener 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 System.Diagnostics.ConsoleTraceListener.

The following example adds a System.Diagnostics.ConsoleTraceListener object named configConsoleListener to the Trace.Listeners collection.

Example

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        

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

Requirements

Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0