Directs tracing or debugging output to either the standard output or the standard error stream.
Assembly: System (in System.dll)
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
When trace and debugging output is enabled, the 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.
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 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.
The Debug and Trace classes share the same TraceListenerCollection 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 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 object to the Listeners collection in your code, or you can add a object to the Listeners 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 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 object from the Listeners collection, and call the Close 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 Visual Studio 2005 project. In this file, insert an element for a .
The following example adds a 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>.
The following code example implements a console application consisting of a class with two public methods.
The Main method examines the command-line arguments and determines if trace output should be directed to the standard error stream or the standard output stream. Main creates and initializes a object for the specified Console output stream, and adds this object to the trace listener collection. It then calls the WriteEnvironmentInfoToTrace method, which writes details about the executing environment and the trace listener configuration to the trace output.
When the example application runs, the environment and trace configuration details are written to the specified console output stream through the object.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.