Use the ConsoleTraceListener class to write trace and debugging messages to the console. You can initialize a 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 ConsoleTraceListener messages are written to the specified System..::.Console stream, which is similar to the way messages are written with the Console..::.Write or Console..::.WriteLine methods. In a console application, the System..::.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 displa
yed.
|
Add a ConsoleTraceListener object to the appropriate Listeners collection if you want messages written through Trace, TraceSource, or Debug to be written to the console. In addition, you can write messages directly to the console using the Trace..::.Write or Trace..::.WriteLine methods.
Note: |
|---|
The
Debug and Trace classes share the same TraceListenerCollection collection, accessed through their respective Listeners properties. If you add a 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.
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 ConsoleTraceListener object to the Listeners collection in your code, or you can add a ConsoleTraceListener object to the Listeners collection through the application configuration file. Add the ConsoleTraceListener object in your code to write messages for a specific code section or execution path. Add the 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 ConsoleTraceListener object and add it to the Listeners collection. Instrument the section of code that contains messages using the Trace or Debug classes. At the end of the code section, remove the ConsoleTraceListener object from the Listeners collection, and call the Close method on the ConsoleTraceListener.
To direct all trace and debug messages to the console while the application executes, add a 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 Visual Studio 2005 project. In this file, insert an element for a ConsoleTraceListener.
The following example adds a ConsoleTraceListener object named configConsoleListener to the Listeners 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 for <trace>.