Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

Debug Class

Provides a set of methods and properties that help debug your code. This class cannot be inherited.

For a list of all members of this type, see Debug Members.

System.Object
   System.Diagnostics.Debug

[Visual Basic]
NotInheritable Public Class Debug
[C#]
public sealed class Debug
[C++]
public __gc __sealed class Debug
[JScript]
public class Debug

Thread Safety

This type is safe for multithreaded operations.

Remarks

If you use methods in the Debug class to print debugging information and check your logic with assertions, you can make your code more robust without impacting the performance and code size of your shipping product.

In Visual Studio .NET projects, creating a debug build enables Debug. For information on how to disable Debug, see the Visual Studio .NET documentation.

In contrast, in Visual Studio .NET projects, Trace is enabled by default for both release and debug builds, so code is generated for all trace methods in both release and debug builds. Therefore, you can use Trace to instrument release builds.

This class provides methods to display an Assert dialog box, and to emit an assertion that will always fail. This class provides write methods in the following variations: Write, WriteLine, WriteIf and WriteLineIf.

The BooleanSwitch and TraceSwitch classes provide means to dynamically control the tracing output. You can modify the values of these switches without recompiling your application. For information on using the configuration file to set a switch, see the Switch class and the TraceSwitch Configuration topic in the Visual Studio .NET documentation.

You can customize the tracing output's target by adding TraceListener instances to or removing instances from the Listeners collection. By default, the DefaultTraceListener class emits trace output.

You can modify the level of indentation using the Indent method or the IndentLevel property. To modify the indent spacing, use the IndentSize property. You can specify whether to automatically flush the output buffer after each write by setting the AutoFlush property to true.

To set the AutoFlush and IndentSize for Debug, you can edit the configuration file corresponding to the name of your application. The configuration file should be formatted like the following example:

<configuration>
    <system.diagnostics>
       <trace autoflush="true" indentsize="7" />
    </system.diagnostics>
 </configuration>
Note   To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or add #define DEBUG to the top of your file. In Visual Basic, add the /d:DEBUG=True flag to the compiler command line. To provide equivalent functionality in the Managed Extensions for C++, you must enclose calls to methods of this class in a #ifdef DEBUG ... #endif block. This syntax is compiler-specific. If you are using a compiler other than the ones specified above, you must refer to the compiler's documentation to enable conditional compiling because of the conditional compilation attributes placed on the methods of Debug.

Example

The following example uses Debug to indicate the beginning and end of a program's execution. The example also uses Indent and Unindent to distinguish the tracing output.

[Visual Basic] 
Public Shared Function Main(args() As String) As Integer
    Debug.Listeners.Add(New TextWriterTraceListener(Console.Out))
    Debug.AutoFlush = True
    Debug.Indent()
    Debug.WriteLine("Entering Main")
    Console.WriteLine("Hello World.")
    Debug.WriteLine("Exiting Main")
    Debug.Unindent()
    Return 0
End Function 'Main

[C#] 
public static int Main(string[] args)
{
   Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
   Debug.AutoFlush = true;
   Debug.Indent();
   Debug.WriteLine("Entering Main");
   Console.WriteLine("Hello World.");
   Debug.WriteLine("Exiting Main"); 
   Debug.Unindent();
   return 0;
}


[C++] 
#using <mscorlib.dll>
#using <System.dll>

int main(void)
{
    using namespace System;
    using namespace System::Diagnostics;

    Debug::Listeners->Add(new TextWriterTraceListener(Console::Out));
    Debug::AutoFlush = true;
    Debug::Indent();
    Debug::WriteLine(S"Entering Main");

    Console::WriteLine(S"Hello World.");

    Debug::WriteLine(S"Exiting Main");

    Debug::Unindent();
    return 0;
}

[JScript] 
public function Main() : int
{
   Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
   Debug.AutoFlush = true;
   Debug.Indent();
   Debug.WriteLine("Entering Main");
   Console.WriteLine("Hello World.");
   Debug.WriteLine("Exiting Main"); 
   Debug.Unindent();
   return 0;
}

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, .NET Compact Framework

Assembly: System (in System.dll)

See Also

Debug Members | System.Diagnostics Namespace | Trace | Switch | BooleanSwitch | TraceSwitch | TraceListener | DefaultTraceListener | EventLogTraceListener | TraceListenerCollection | ConditionalAttribute

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.