程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 如何在Visual C#.NET中跟蹤和調試

如何在Visual C#.NET中跟蹤和調試

編輯:關於C#

當程序運行時,您可以使用 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("Output.txt"));
Debug.Listeners.Add(tr2);

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();

確認它可以使用

1.確保 Debug 是當前的解決方案配置。

2.如果“解決方案資源管理器”窗口不可見,請按 CTRL+ALT+L 組合鍵以顯示此窗口。

3.右鍵單擊“conInfo”,然後單擊“屬性”。

4.在 conInfo 屬性頁左窗格中,在“配置”文件夾下,請確保箭頭指向“調試”。

5.在“配置”文件夾上面的“配置”下拉列表框中,單擊“活動(調試)”或“調試”,然後單擊“確定”。

6.按 CTRL+ALT+O 以顯示“輸出”窗口。

7.按 F5 鍵以運行該代碼。在出現“斷言失敗”對話框時,單擊“忽略”。

8.在“控制台”窗口中,按 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) \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

9.“控制台”窗口和 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 名稱空間。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved