本文將告訴您,如何使用
Debug 和
Trace 類別。 在 Microsoft .NET Framework 中可使用這兩個類別。 您可以在應用程式開發過程中,或是實際執行的部署完成後,使用這些類別以提供有關應用程式效能的資訊。 這些類別只佔一部份 .NET Framework 中可使用的工具功能。
需求
下面的清單簡介建議的硬體、軟體、網路基礎架構和您需要的 Service Pack:
- Microsoft Windows 2000 或 Microsoft Windows XP
- Microsoft Visual C# .NET
本文也假設您對程式偵錯是熟悉的。
技術說明
<用 Debug 類別建立範例>一節中的步驟將示範如何建立一個使用
Debug 類別,以提供有關程式執行資訊的控制台應用程式。
執行程式的時候,您可以使用
Debug 類別的方法產生訊息以協助監控程式執行順序、偵測異常或提供效能評估資訊。 依照預設,
Debug 類別產生的訊息會出現在 Visual Studio 整合式開發環境 (IDE,Integrated Development Environment) 的 [輸出] 視窗中。
程式碼範例使用
WriteLine 方法產生一個訊息,訊息後有斷行符號。 使用此方法產生訊息時,每一訊息在 [輸出] 視窗都顯示於個別的一行中。
使用
Debug 類別的
Assert 方法時,只有指定的條件估算為 false 時,[輸出] 視窗才會顯示一段訊息。 這段訊息也會出現在強制回應的對話方塊中讓使用者看見。 此對話方塊包含訊息、專案名稱和
Debug.Assert 陳述式號碼。 此對話方塊也包含下列三個指令按鈕:
- [終止]: 應用程式停止執行。
- [重試]: 應用程式進入偵錯模式。
- [忽略]: 應用程式繼續執行。
使用者必須按下這些按鈕其中之一應用程式才會繼續執行。
您也可以從
Debug 類別將輸出導向到 [輸出] 視窗以外的目的地。
Debug 類別擁有一個名稱為
Listeners 的集合,包含有
Listener 物件。
每一個
Listener 物件都會監控
Debug 的輸出,並將輸出導向到指定的目標。
每一個
Listener 集合中的
Listener 都會接收到任何
Debug 類別產生的輸出。 使用
TextWriterTraceListener 類別定義
Listener 物件。 您可以用
TextWriterTraceListener 類別的建構函式指定其目的地。
下列為可能的輸出目標:
- 使用 System.Console.Out 屬性的 [主控台] 視窗。
- 使用 System.IO.File.CreateText("FileName.txt") 陳述式的文字 (.txt) 檔。
建立
TextWriterTraceListener 物件後,必須將物件新增至
Debug.Listeners 集合中,以接收 Debug 輸出。
用 Debug 類別建立範例
- 啟動 Visual Studio .NET。
- 建立一個名為 conInfo 的新 Visual C# .NET 主控台應用程式專案。 此時 Class1 已建立。
- 在 Class1 的最上端加入下列命名空間。
using System.Diagnostics;
- 若要將變數初始化以包含有關產品的資訊,把下列的宣告陳述式新增至 Main 方法中:
string sProdName = "Widget";
int iUnitQty = 100;
double dUnitCost = 1.03;
- 將該類別產生的訊息指定為 WriteLine 方法的第一個輸入參數。 按下 CTRL+ALT+O 以確定可以看到 [輸出] 視窗。
Debug.WriteLine("Debug Information-Product Starting "); - 為方便閱讀,利用 Indent 方法將 [輸出] 視窗中接下來的訊息縮排:
- 若要顯示選取的變數內容,請如下使用 WriteLine 方法:
Debug.WriteLine("The product name is " + sProdName);
Debug.WriteLine("The available units on hand are" + iUnitQty.ToString());
Debug.WriteLine("The per unit cost is " + dUnitCost.ToString()); - 您也可以使用 WriteLine 方法顯示命名空間和現有物件的類別名稱。 例如,下面程式碼在 [輸出] 視窗中顯示 System.Xml.XmlDocument 命名空間:
System.Xml.XmlDocument oxml = new System.Xml.XmlDocument();
Debug.WriteLine(oxml);
- 若要組織輸出,您可以在 WriteLine 方法中包含一個類別,以作為選擇性的、次要的輸入變數。 如果您指定一個類別,[輸出] 視窗中的訊息格式會是 category:message, 例如,下列程式碼的第一行在 [輸出] 視窗顯示為 Field: The product name is Widget:
Debug.WriteLine("The product name is " + sProdName,"Field");
Debug.WriteLine("The units on hand are" + iUnitQty,"Field");
Debug.WriteLine("The per unit cost is" + dUnitCost.ToString(),"Field");
Debug.WriteLine("Total Cost is " + (iUnitQty * dUnitCost),"Calc"); - 只有在使用 Debug 類別的 WriteLineIf 方法估算指定的條件,而結果為 true 的時候,[輸出] 視窗才能顯示訊息。 WriteLineIf 方法的第一個輸入參數是要估算的條件。 WriteLineIf 的第二個參數是只有在第一個參數條件估算為 true 的時候才顯示的訊息。
Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear");
Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear");
- 使用 Debug 類別的 Assert 方法,則只有在指定的條件估算為 false 的時候 [輸出] 視窗才會顯示訊息:
Debug.Assert(dUnitCost > 1, "Message will NOT appear");
Debug.Assert(dUnitCost < 1, "Message will appear since dUnitcost < 1 is false");
- 為 [主控台] 視窗 (tr1) 和文字檔 Output.txt (tr2) 建立 TextWriterTraceListener 物件,再將每個物件新增至 Debug Listeners 集合:
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(tr1);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt"));
Debug.Listeners.Add(tr2); - 為方便閱讀,請使用 Unindent 方法,將 Debug 類別後續產生的訊息中的縮排移除。 同時使用 Indent 和 Unindent 方法時,讀取器可以辨識不同群組的輸出。
Debug.Unindent();
Debug.WriteLine("Debug Information-Product Ending"); - 若要確定每個 Listener 物件都接收到所有輸出,呼叫 Flush 方法以對 Debug 類別緩衝區做處理:
使用 Trace 類別
您也可以使用
Trace 類別以產生監控應用程式執行的訊息。
Trace 和
Debug 類別大部分共用相同的方法以產生輸出,包括:
- WriteLine
- WriteLineIf
- Indent
- Unindent
- Assert
- Flush
您可以在同一應用程式中分別或同時使用
Trace 和
Debug 類別。 在 Debug Solution Configuration 專案中,
Trace 和
Debug 輸出都是啟動的。 本專案會從這兩個類別中產生輸出至所有的
Listener 物件。 然而,Release Solution Configuration 專案只會從
Trace 類別產生輸出。 Release Solution Configuration 忽略任何
Debug 類別方法的叫用。
Trace.WriteLine("Trace Information-Product Starting ");
Trace.Indent();
Trace.WriteLine("The product name is "+sProdName);
Trace.WriteLine("The product name is"+sProdName,"Field" );
Trace.WriteLineIf(iUnitQty > 50, "This message WILL appear");
Trace.Assert(dUnitCost > 1, "Message will NOT appear");
Trace.Unindent();
Trace.WriteLine("Trace Information-Product Ending");
Trace.Flush();
Console.ReadLine();
確認可運作
- 請確定 Debug 是現在的方案組態。
- 如果看不到 [方案總管] 視窗,按下 CTRL+ALT+L 按鍵組合以顯示此視窗。
- 用滑鼠右鍵按一下 [conInfo],再按一下 [屬性]。
- 在 [conInfo] 屬性頁的左邊窗格中,[組態] 資料夾下,請確認箭頭是指向 [偵錯]。
- 在 [組態] 資料夾上方,[組態] 下拉式清單中,按一下 [啟動 (偵錯)] 或 [偵錯],再按一下 [確定]。
- 按下 CTRL+ALT+O 以顯示 [輸出] 視窗。
- 按 F5 鍵以執行此程式碼。 當 [Assertion Failed] 對話方塊出現時,按一下 [忽略]。
- 在 [主控台] 視窗中,按下 ENTER。 程式應該會結束,且 [輸出] 視窗應該會顯示下列輸出:
Debug Information-Product Starting
The product name is Widget
The available units on hand are100
The per unit cost is 1.03
System.Xml.XmlDocument
Field: The product name is Widget
Field: The units on hand are100
Field: The per unit cost is1.03
Calc: Total Cost is 103
This message WILL appear
---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
Message will appear since dUnitcost < 1 is false
---- Assert Long Message ----
at Class1.Main(String[] args) <%Path%>\class1.cs(34)
The product name is Widget
The available units on hand are100
The per unit cost is 1.03
Debug Information-Product Ending
Trace Information-Product Starting
The product name is Widget
Field: The product name isWidget
This message WILL appear
Trace Information-Product Ending
- [主控台] 視窗和 Output.txt 檔應該會顯示下列輸出:
The product name is Widget
The available units on hand are 100
The per unit cost is 1.03
Debug Information-Product Ending
Trace Information-Product Starting
The product name is Widget
Field: The product name is Widget
This message WILL appear
Trace Information-Product Ending
注意 Output.txt 檔與 conInfo 執行檔 (conInfo.exe) 位於同一目錄。 這是 \bin 資料夾,通常專案程式碼存放在這裡。 依照預設,這是 C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin。
完整程式碼
列表
using System;
using System.Diagnostics;
class Class1
{
[STAThread]
static void Main(string[] args)
{
string sProdName = "Widget";
int iUnitQty = 100;
double dUnitCost = 1.03;
Debug.WriteLine("Debug Information-Product Starting ");
Debug.Indent();
Debug.WriteLine("The product name is "+sProdName);
Debug.WriteLine("The available units on hand are"+iUnitQty.ToString());
Debug.WriteLine("The per unit cost is "+ dUnitCost.ToString());
System.Xml.XmlDocument oxml = new System.Xml.XmlDocument();
Debug.WriteLine(oxml);
Debug.WriteLine("The product name is "+sProdName,"Field");
Debug.WriteLine("The units on hand are"+iUnitQty,"Field");
Debug.WriteLine("The per unit cost is"+dUnitCost.ToString(),"Field");
Debug.WriteLine("Total Cost is "+(iUnitQty * dUnitCost),"Calc");
Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear");
Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear");
Debug.Assert(dUnitCost > 1, "Message will NOT appear");
Debug.Assert(dUnitCost < 1, "Message will appear since dUnitcost < 1 is false");
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(tr1);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt"));
Debug.Listeners.Add(tr2);
Debug.WriteLine("The product name is "+sProdName);
Debug.WriteLine("The available units on hand are"+iUnitQty);
Debug.WriteLine("The per unit cost is "+dUnitCost);
Debug.Unindent();
Debug.WriteLine("Debug Information-Product Ending");
Debug.Flush();
Trace.WriteLine("Trace Information-Product Starting ");
Trace.Indent();
Trace.WriteLine("The product name is "+sProdName);
Trace.WriteLine("The product name is"+sProdName,"Field" );
Trace.WriteLineIf(iUnitQty > 50, "This message WILL appear");
Trace.Assert(dUnitCost > 1, "Message will NOT appear");
Trace.Unindent();
Trace.WriteLine("Trace Information-Product Ending");
Trace.Flush();
Console.ReadLine();
} } 注意 如果要使此程式碼範例運作,必須將
using System.Diagnostics; 貼入程式碼作為首行,以新增
System.Diagonstics
命名空間。
疑難排解
- 如果方案組態類型是 Release,就會忽略Debug 類別輸出。
- 在為特定的目標建立 TextWriterTraceListener 類別後,TextWriterTraceListener 會從 Trace 和 Debug 類別收到輸出。 不論您是用 Trace 或是 Debug 類別的 Add 方法將 TextWriterTraceListener 新增至 Listeners 類別,這情形都會發生。
- 如果您將 Listeners 物件新增至 Trace 和 Debug 類別中的同一目標,則不論輸出是由 Debug 或 Trace 產生,都會複製輸出的每一行。
TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(myWriter);
TextWriterTraceListener myCreator = new TextWriterTraceListener(System.Console.Out);
Trace.Listeners.Add(myCreator);
如需詳細資訊,請參閱下列 .NET Framework Class Library 文件的主題:
如需有關追蹤的詳細資訊,請參閱下列 Microsoft GotDotNet Quickstart 教學課程的主題: