摘要:
怎樣在 Visual C# .NET 中跟蹤和調試?當程序運行時,您可以使用 Debug 類的方法來生成消息,以幫助您監視程序執行順序、檢測故障或提供性能度量信息。默認情況下,Debug 類產生的消息顯示在 Visual Studio 集成開發環境 (IDE) 的“輸出”窗口中。
如何使用 Debug
當程序運行時,您可以使用 Debug 類的方法來生成消息,以幫助您監視程序執行順序、檢測故障或提供性能度量信息。默認情況下,Debug 類產生的消息顯示在 Visual Studio 集成開發環境 (IDE) 的“輸出”窗口中。
該代碼示例使用 WriteLine 方法生成后面帶有行結束符的消息。當您使用此方法生成消息時,每條消息在“輸出”窗口中均顯示為單獨的一行。
使用 Debug 類創建一個示例
1. 啟動 Visual Studio .NET。
2. 新建一個名為 conInfo 的新 Visual C# .NET 控制台應用程序項目。將創建 Class1。
3. 在 Class1 的頂部添加以下名稱空間。
using System.Diagnostics;
4. 要初始化變量以使其包含產品的相關信息,請將下面的聲明語句添加到 Main 方法:
string sProdName = "Widget"; int iUnitQty = 100; double dUnitCost = 1.03;
5. (就在上面代碼后面)直接輸入將類生成的消息指定為 WriteLine 方法的第一個輸入參數。按 CTRL+ALT+O 組合鍵以確保“輸出”窗口可見。
Debug.WriteLine("Debug Information-Product Starting ");
6. 為了清晰易讀,請使用 Indent 方法在“輸出”窗口中縮進后面的消息:
Debug.Indent();
7. 要顯示所選變量的內容,請使用 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());
8. 您還可以使用 WriteLine 方法顯示現有對象的名稱空間和類名稱。例如,下面的代碼在“輸出”窗口中顯示 System.Xml.XmlDocument 命名空間:
System.Xml.XmlDocument oxml = new System.Xml.XmlDocument(); Debug.WriteLine(oxml);
9、要整理輸出,可以包括一個類別作為 WriteLine 方法的第二個可選的輸入參數。如果您指定一個類別,則“輸出”窗口消息的格式為“類別:消息”。例如,以下代碼的第一行在“輸出”窗口中顯示“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");
10. 僅在使用 Debug 類的 WriteLineIf 方法將指定條件計算為 true 時,“輸出”窗口才可以顯示消息。將要計算的條件是 WriteLineIf 方法的第一個輸入參數。WriteLineIf 的第二個參數是僅在第一個參數的條件計算為真時才顯示的消息。
Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear"); Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear");
11. 使用 Debug 類的 Assert 方法,使“輸出”窗口僅在指定條件計算為 false 時才顯示消息:
Debug.Assert(dUnitCost > 1, "Message will NOT appear"); Debug.Assert(dUnitCost < 1, "Message will appear since dUnitcost < 1 is false");
12. 為“控制台”窗口 (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("d:/Output.txt")); //System.Diagnostics.TextWriterTraceListener tr2 = new System.Diagnostics.TextWriterTraceListener(@"d:/Output.txt");
//System.Diagnostics.TextWriterTraceListener tr2 = new System.Diagnostics.TextWriterTraceListener(@"Output.txt"); Debug.Listeners.Add(tr2);
說明:
tr1定義:將調試信息,即Debug輸出通過控制台程序輸出。
tr2定義,將調試信息,即Debug內容輸出到Output.txt文件,通常需要tr2.Flush()才能輸出到文本文件,或者調用Debug.Flush(),將全部TextWriterTraceListener對象的調試緩沖輸出。
不指定路徑盤符,則輸出的調試文件保存在程序的運行目錄下。
13. 為了清晰易讀,請使用 Unindent 方法去除 Debug 類為后續消息生成的縮進。當您將 Indent 和 Unindent 兩種方法一起使用時,讀取器可以將輸出分成組。
Debug.Unindent(); Debug.WriteLine("Debug Information-Product Ending");
14. 為了確保每個 Listener 對象收到它的所有輸出,請為 Debug 類緩沖區調用 Flush 方法:
Debug.Flush();
使用 Trace 類
您還可以使用 Trace 類生成監視應用程序執行的消息。Trace 和 Debug 類共享大多數相同的方法來生成輸出,這些方法包括:
◆WriteLine
◆WriteLineIf
◆Indent
◆Unindent
◆Assert
◆Flush
您可以在同一應用程序中分別或同時使用 Trace 和 Debug 類。在一個“調試解決方案配置”項目中,Trace 和 Debug 兩種輸出均為活動狀態。該項目從這兩個類為 Listener 對象生成輸出。但是,“發布解決方案配置”項目僅從 Trace 類生成輸出。該“發布解決方案配置”項目忽略任何 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();
System.Diagnostics.Trace.WriteLine("Trace Log"); System.Diagnostics.Trace.Listeners.Clear(); System.Diagnostics.Trace.AutoFlush = true; System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener("app.log"));
