Debug Debug Debug Debug Class

定義

提供一組方法和屬性,可協助偵錯程式碼。Provides a set of methods and properties that help debug your code.

public ref class Debug abstract sealed
public static class Debug
type Debug = class
Public Class Debug
繼承
DebugDebugDebugDebug

範例

下列範例會使用Debug來指出程式執行的開始和結束。The following example uses Debug to indicate the beginning and end of a program's execution. 此範例也會Indent使用Unindent和來區別追蹤輸出。The example also uses Indent and Unindent to distinguish the tracing output.

// Specify /DDEBUG when compiling.

#using <System.dll>
using namespace System;
using namespace System::Diagnostics;

int main( void )
{
   #if defined(DEBUG)
   Debug::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Debug::AutoFlush = true;
   Debug::Indent();
   Debug::WriteLine( "Entering Main" );
   #endif
   Console::WriteLine( "Hello World." );
   #if defined(DEBUG)
   Debug::WriteLine( "Exiting Main" );
   Debug::Unindent();
   #endif
   return 0;
}
// Specify /d:DEBUG when compiling.

using System;
using System.Data;
using System.Diagnostics;

class Test
{
    static void Main()
    {
       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();
    }
}
' Specify /d:DEBUG=True when compiling.

Imports System.Data
Imports System.Diagnostics

Class Test

    Shared Sub Main()
    
        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()
        
    End Sub
    
End Class

備註

如果您使用類別中的Debug方法來列印偵錯工具資訊, 並使用判斷提示來檢查邏輯, 您可以讓程式碼更穩固, 而不會影響出貨產品的效能和程式碼大小。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.

這個類別會提供方法來顯示Assert對話方塊, 以及發出一律會失敗的判斷提示。This class provides methods to display an Assert dialog box, and to emit an assertion that will always fail. 這個類別會在下列變化中提供 write 方法Write: WriteLine WriteIf 、和WriteLineIfThis class provides write methods in the following variations: Write, WriteLine, WriteIf and WriteLineIf.

BooleanSwitchTraceSwitch類別提供動態控制追蹤輸出的方法。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. 如需使用設定檔來設定交換器的詳細資訊, 請參閱Switch類別和追蹤參數主題。For information on using the configuration file to set a switch, see the Switch class and the Trace Switches topic.

您可以在TraceListener Listeners集合中加入實例或移除實例, 以自訂追蹤輸出的目標。You can customize the tracing output's target by adding TraceListener instances to or removing instances from the Listeners collection. 集合是由DebugTrace類別共用; 將追蹤接聽項新增至其中一個類別, 會將接聽程式新增至兩者。 ListenersThe Listeners collection is shared by both the Debug and the Trace classes; adding a trace listener to either class adds the listener to both. 根據預設, DefaultTraceListener類別會發出追蹤輸出。By default, the DefaultTraceListener class emits trace output.

注意

如果追蹤接聽項所使用Listeners的資源無法使用, 則在追蹤時, 將追蹤接聽項加入至集合可能會導致例外狀況擲回。Adding a trace listener to the Listeners collection can cause an exception to be thrown while tracing, if a resource used by the trace listener is not available. 條件和擲回的例外狀況取決於追蹤接聽項, 而且無法在本主題中列舉。The conditions and the exception thrown depend on the trace listener and cannot be enumerated in this topic. Debug區塊中try 呼叫方法來catch偵測並處理追蹤接聽程式的任何例外狀況, 可能會很有用。 /It may be useful to place calls to the Debug methods in try/catch blocks to detect and handle any exceptions from trace listeners.

您可以使用Indent方法IndentLevel或屬性來修改縮排的層級。You can modify the level of indentation using the Indent method or the IndentLevel property. 若要修改縮排間距, 請IndentSize使用屬性。To modify the indent spacing, use the IndentSize property. 您可以將AutoFlush屬性設定為true, 以指定是否要在每次寫入之後自動清除輸出緩衝區。You can specify whether to automatically flush the output buffer after each write by setting the AutoFlush property to true.

若要設定AutoFlush IndentSize和, 您可以編輯對應至應用程式名稱的設定檔。 DebugTo 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 as shown in the following example.

<configuration>  
  <system.diagnostics>  
    <trace autoflush="true" indentsize="7" />  
  </system.diagnostics>  
</configuration>  

屬性會套用至的Debug方法。 ConditionalAttributeThe ConditionalAttribute attribute is applied to the methods of Debug. 支援ConditionalAttribute忽略這些方法之呼叫的編譯器, 除非 "DEBUG" 定義為條件式編譯符號。Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol. 請參閱編譯器的檔, 以判斷是否ConditionalAttribute支援, 以及定義條件式編譯符號的語法。Refer to a compiler's documentation to determine whether ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol.

注意

在 Visual Studio C#和 Visual Basic 專案中, 預設會針對 debug 組建定義 "debug" 條件式編譯符號, 並針對 debug 和 release 組建定義 "TRACE" 符號。In Visual Studio C# and Visual Basic projects, by default, the "DEBUG" conditional compilation symbol is defined for debug builds, and the "TRACE" symbol is defined for both debug and release builds. 如需有關如何停用此行為的詳細資訊, 請參閱 Visual Studio 檔。For information about how to disable this behavior, see the Visual Studio documentation. 如需在 Visual C++中進行條件式調試的詳細資訊, 請參閱Debug Class (C++/cli)For information about conditional debugging in Visual C++, see Debug Class (C++/CLI).

若要在中C#定義「DEBUG」條件式編譯符號, /d:DEBUG請在您使用命令列編譯器代碼時, 將選項加入至編譯器命令列#define DEBUG , 或將新增至檔案頂端。To define the "DEBUG" conditional compilation symbol in C#, add the /d:DEBUG option to the compiler command line when you compile your code using a command line, or add #define DEBUG to the top of your file. 在 Visual Basic 中, 將/d:DEBUG=True選項新增至編譯器命令列, 或#Const DEBUG=True新增至檔案。In Visual Basic, add the /d:DEBUG=True option to the compiler command line or add #Const DEBUG=True to the file.

屬性

AutoFlush AutoFlush AutoFlush AutoFlush

取得或設定數值,表示每次寫入後是否要在 Flush() 上呼叫 ListenersGets or sets a value indicating whether Flush() should be called on the Listeners after every write.

IndentLevel IndentLevel IndentLevel IndentLevel

取得或設定縮排層級。Gets or sets the indent level.

IndentSize IndentSize IndentSize IndentSize

取得或設定縮排的空格數目。Gets or sets the number of spaces in an indent.

Listeners Listeners Listeners Listeners

取得監視偵錯輸出的接聽項的集合。Gets the collection of listeners that is monitoring the debug output.

方法

Assert(Boolean) Assert(Boolean) Assert(Boolean) Assert(Boolean)

檢查條件;如果條件為 false,則會顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, displays a message box that shows the call stack.

Assert(Boolean, String) Assert(Boolean, String) Assert(Boolean, String) Assert(Boolean, String)

檢查條件;如果條件為 false,則會輸出指定的訊息並且顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.

Assert(Boolean, String, String) Assert(Boolean, String, String) Assert(Boolean, String, String) Assert(Boolean, String, String)

檢查條件;如果條件為 false,則輸出兩個指定的訊息,並顯示訊息方塊,該方塊會顯示呼叫堆疊。Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

Assert(Boolean, String, String, Object[]) Assert(Boolean, String, String, Object[]) Assert(Boolean, String, String, Object[]) Assert(Boolean, String, String, Object[])

檢查條件;如果條件為 false,則會輸出兩個訊息 (簡單和格式化) 並且顯示列出呼叫堆疊的訊息方塊。Checks for a condition; if the condition is false, outputs two messages (simple and formatted) and displays a message box that shows the call stack.

Close() Close() Close() Close()

清除輸出緩衝區,然後對每個 Listeners 呼叫 Close 方法。Flushes the output buffer and then calls the Close method on each of the Listeners.

Fail(String) Fail(String) Fail(String) Fail(String)

發出指定的錯誤訊息。Emits the specified error message.

Fail(String, String) Fail(String, String) Fail(String, String) Fail(String, String)

發出錯誤訊息和詳細錯誤訊息。Emits an error message and a detailed error message.

Flush() Flush() Flush() Flush()

清除輸出緩衝區並造成緩衝資料寫入 Listeners 集合。Flushes the output buffer and causes buffered data to write to the Listeners collection.

Indent() Indent() Indent() Indent()

將目前的 IndentLevel 增加一級。Increases the current IndentLevel by one.

Print(String) Print(String) Print(String) Print(String)

將後面接著行結束字元的訊息寫入 Listeners 集合中的追蹤接聽項。Writes a message followed by a line terminator to the trace listeners in the Listeners collection.

Print(String, Object[]) Print(String, Object[]) Print(String, Object[]) Print(String, Object[])

將後面接著行結束字元的格式化字串寫入 Listeners 集合中的追蹤接聽項。Writes a formatted string followed by a line terminator to the trace listeners in the Listeners collection.

Unindent() Unindent() Unindent() Unindent()

將目前的 IndentLevel 減少一級。Decreases the current IndentLevel by one.

Write(String, String) Write(String, String) Write(String, String) Write(String, String)

將分類名稱和訊息寫入 Listeners 集合中的追蹤接聽項。Writes a category name and message to the trace listeners in the Listeners collection.

Write(Object, String) Write(Object, String) Write(Object, String) Write(Object, String)

將分類名稱和物件之 ToString() 方法的值寫入 Listeners 集合中的追蹤接聽項。Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection.

Write(Object) Write(Object) Write(Object) Write(Object)

將物件的 ToString() 方法的值寫入 Listeners 集合中的追蹤接聽項。Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.

Write(String) Write(String) Write(String) Write(String)

將訊息寫入 Listeners 集合中的追蹤接聽項。Writes a message to the trace listeners in the Listeners collection.

WriteIf(Boolean, Object) WriteIf(Boolean, Object) WriteIf(Boolean, Object) WriteIf(Boolean, Object)

如果條件為 true,則將物件之 ToString() 方法的值寫入 Listeners 集合中的追蹤接聽項。Writes the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.

WriteIf(Boolean, String) WriteIf(Boolean, String) WriteIf(Boolean, String) WriteIf(Boolean, String)

如果條件為 true,則將訊息寫入 Listeners 集合中的追蹤接聽項。Writes a message to the trace listeners in the Listeners collection if a condition is true.

WriteIf(Boolean, Object, String) WriteIf(Boolean, Object, String) WriteIf(Boolean, Object, String) WriteIf(Boolean, Object, String)

如果條件為 true,則將分類名稱和物件之 ToString() 方法的值寫入 Listeners 集合中的追蹤接聽項。Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.

WriteIf(Boolean, String, String) WriteIf(Boolean, String, String) WriteIf(Boolean, String, String) WriteIf(Boolean, String, String)

如果條件為 true,則將分類名稱和訊息寫入 Listeners 集合中的追蹤接聽項。Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.

WriteLine(Object) WriteLine(Object) WriteLine(Object) WriteLine(Object)

將物件的 ToString() 方法的值寫入 Listeners 集合中的追蹤接聽項。Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.

WriteLine(String) WriteLine(String) WriteLine(String) WriteLine(String)

將後面接著行結束字元的訊息寫入 Listeners 集合中的追蹤接聽項。Writes a message followed by a line terminator to the trace listeners in the Listeners collection.

WriteLine(Object, String) WriteLine(Object, String) WriteLine(Object, String) WriteLine(Object, String)

將分類名稱和物件之 ToString() 方法的值寫入 Listeners 集合中的追蹤接聽項。Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection.

WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[])

將後面接著行結束字元的格式化訊息寫入 Listeners 集合中的追蹤接聽項。Writes a formatted message followed by a line terminator to the trace listeners in the Listeners collection.

WriteLine(String, String) WriteLine(String, String) WriteLine(String, String) WriteLine(String, String)

將分類名稱和訊息寫入 Listeners 集合中的追蹤接聽項。Writes a category name and message to the trace listeners in the Listeners collection.

WriteLineIf(Boolean, Object, String) WriteLineIf(Boolean, Object, String) WriteLineIf(Boolean, Object, String) WriteLineIf(Boolean, Object, String)

如果條件為 true,則將分類名稱和物件之 ToString() 方法的值寫入 Listeners 集合中的追蹤接聽項。Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.

WriteLineIf(Boolean, Object) WriteLineIf(Boolean, Object) WriteLineIf(Boolean, Object) WriteLineIf(Boolean, Object)

如果條件為 true,則將物件之 ToString() 方法的值寫入 Listeners 集合中的追蹤接聽項。Writes the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.

WriteLineIf(Boolean, String) WriteLineIf(Boolean, String) WriteLineIf(Boolean, String) WriteLineIf(Boolean, String)

如果條件為 true,則將訊息寫入 Listeners 集合中的追蹤接聽項。Writes a message to the trace listeners in the Listeners collection if a condition is true.

WriteLineIf(Boolean, String, String) WriteLineIf(Boolean, String, String) WriteLineIf(Boolean, String, String) WriteLineIf(Boolean, String, String)

如果條件為 true,則將分類名稱和訊息寫入 Listeners 集合中的追蹤接聽項。Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.

適用於

執行緒安全性

此型別具備執行緒安全。This type is thread safe.

另請參閱