熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

怎樣在Visual C# .NET 中跟蹤和調試(1)

2022-06-13   來源: .NET編程 

  本文介紹如何使用 Debug

  當程序運行時您可以使用 Debug 類的方法來生成消息以幫助您監視程序執行順序檢測故障或提供性能度量信息默認情況下Debug 類產生的消息顯示在 Visual Studio 集成開發環境 (IDE) 的輸出窗口中

  該代碼示例使用 WriteLine 方法生成後面帶有行結束符的消息當您使用此方法生成消息時每條消息在輸出窗口中均顯示為單獨的一行

  使用 Debug 類創建一個示例
啟動 Visual Studio NET

   新建一個名為 conInfo 的新 Visual C# NET 控制台應用程序項目將創建 Class

   在 Class 的頂部添加以下名稱空間

  using SystemDiagnostics;

   要初始化變量以使其包含產品的相關信息請將下面的聲明語句添加到 Main 方法

  string sProdName = Widget;
int iUnitQty = ;
double dUnitCost = ;

   (就在上面代碼後面)直接輸入將類生成的消息指定為 WriteLine 方法的第一個輸入參數按 CTRL+ALT+O 組合鍵以確保輸出窗口可見

  DebugWriteLine(Debug InformationProduct Starting );

   為了清晰易讀請使用 Indent 方法在輸出窗口中縮進後面的消息

  DebugIndent();

   要顯示所選變量的內容請使用 WriteLine 方法如下所示

  DebugWriteLine(The product name is + sProdName);
DebugWriteLine(The available units on hand are + iUnitQtyToString());
DebugWriteLine(The per unit cost is + dUnitCostToString());

   您還可以使用 WriteLine 方法顯示現有對象的名稱空間和類名稱例如下面的代碼在輸出窗口中顯示 SystemXmlXmlDocument 命名空間

  SystemXmlXmlDocument oxml = new SystemXmlXmlDocument();
DebugWriteLine(oxml);

  要整理輸出可以包括一個類別作為 WriteLine 方法的第二個可選的輸入參數如果您指定一個類別輸出窗口消息的格式為類別:消息例如以下代碼的第一行在輸出窗口中顯示Field:The product name is Widget

  DebugWriteLine(The product name is + sProdNameField);
DebugWriteLine(The units on hand are + iUnitQtyField);
DebugWriteLine(The per unit cost is + dUnitCostToString()Field);
DebugWriteLine(Total Cost is + (iUnitQty * dUnitCost)Calc);

   僅在使用 Debug 類的 WriteLineIf 方法將指定條件計算為 true 時輸出窗口才可以顯示消息將要計算的條件是 WriteLineIf 方法的第一個輸入參數WriteLineIf 的第二個參數是僅在第一個參數的條件計算為真時才顯示的消息

  DebugWriteLineIf(iUnitQty > This message WILL appear);
DebugWriteLineIf(iUnitQty < This message will NOT appear);

   使用 Debug 類的 Assert 方法使輸出窗口僅在指定條件計算為 false 時才顯示消息

  DebugAssert(dUnitCost > Message will NOT appear);
DebugAssert(dUnitCost < Message will appear since dUnitcost < is false);

  控制台窗口 (tr) 和名為 Outputtxt (tr) 的文本文件創建 TextWriterTraceListener 對象然後將每個對象添加到 Debug Listeners 集合中

  TextWriterTraceListener tr = new TextWriterTraceListener(SystemConsoleOut);
DebugListenersAdd(tr);
TextWriterTraceListener tr = new TextWriterTraceListener(SystemIOFileCreateText(Outputtxt));
DebugListenersAdd(tr);

   為了清晰易讀請使用 Unindent 方法去除 Debug 類為後續消息生成的縮進當您將 Indent 和 Unindent 兩種方法一起使用時讀取器可以將輸出分成組

  DebugUnindent();
DebugWriteLine(Debug InformationProduct Ending);

   為了確保每個 Listener 對象收到它的所有輸出請為 Debug 類緩沖區調用 Flush 方法

  DebugFlush();


使用 Trace 類

  您還可以使用 Trace 類生成監視應用程序執行的消息Trace 和 Debug 類共享大多數相同的方法來生成輸出這些方法包括

  ◆WriteLine

  ◆WriteLineIf

  ◆Indent

  ◆Unindent

  ◆Assert

  ◆Flush

  您可以在同一應用程序中分別或同時使用 Trace 和 Debug 類在一個調試解決方案配置項目中Trace 和 Debug 兩種輸出均為活動狀態該項目從這兩個類為 Listener 對象生成輸出但是發布解決方案配置項目僅從 Trace 類生成輸出發布解決方案配置項目忽略任何 Debug 類方法調用

  

  TraceWriteLine(Trace InformationProduct Starting ); TraceIndent(); TraceWriteLine(The product name is +sProdName); TraceWriteLine(The product name is+sProdNameField ); TraceWriteLineIf(iUnitQty > This message WILL appear); TraceAssert(dUnitCost > Message will NOT appear); TraceUnindent(); TraceWriteLine(Trace InformationProduct Ending); TraceFlush(); ConsoleReadLine();

  確認它可以使用

   確保 Debug 是當前的解決方案配置
如果解決方案資源管理器窗口不可見請按 CTRL+ALT+L 組合鍵以顯示此窗口
右鍵單擊conInfo然後單擊屬性
在 conInfo 屬性頁左窗格中配置文件夾下請確保箭頭指向調試
配置文件夾上面的配置下拉列表框中單擊活動(調試)調試然後單擊確定
按 CTRL+ALT+O 以顯示輸出窗口
按 F 鍵以運行該代碼在出現斷言失敗對話框時單擊忽略
控制台窗口中按 ENTER 鍵此時程序即已完成輸出窗口應顯示以下輸出

  Debug InformationProduct Starting The product name is Widget The available units on hand are The per unit cost is SystemXmlXmlDocument Field: The product name is Widget Field: The units on hand are Field: The per unit cost is Calc: Total Cost is This message WILL appear DEBUG ASSERTION FAILED Assert Short Message Message will appear since dUnitcost < is false Assert Long Message at ClassMain(String[] args) \classcs() The product name is Widget The available units on hand are The per unit cost is Debug InformationProduct Ending Trace InformationProduct Starting The product name is Widget Field: The product name isWidget This message WILL appear Trace InformationProduct Ending

   控制台窗口和 Outputtxt 文件應顯示以下輸出

  The product name is Widget The available units on hand are The per unit cost is Debug InformationProduct Ending Trace InformationProduct Starting The product name is Widget Field: The product name is Widget This message WILL appear Trace InformationProduct Ending

  注意Outputtxt 文件與 conInfo 可執行文件 (conInfoexe) 位於同一目錄中通常情況下該目錄是存儲項目源的 \bin 文件夾默認情況下為 C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin


完整代碼列表

  using System; using SystemDiagnostics; class Class { [STAThread] static void Main(string[] args) { string sProdName = Widget; int iUnitQty = ; double dUnitCost = ; DebugWriteLine(Debug InformationProduct Starting ); DebugIndent(); DebugWriteLine(The product name is +sProdName); DebugWriteLine(The available units on hand are+iUnitQtyToString()); DebugWriteLine(The per unit cost is + dUnitCostToString()); SystemXmlXmlDocument oxml = new SystemXmlXmlDocument(); DebugWriteLine(oxml); DebugWriteLine(The product name is +sProdNameField); DebugWriteLine(The units on hand are+iUnitQtyField); DebugWriteLine(The per unit cost is+dUnitCostToString()Field); DebugWriteLine(Total Cost is +(iUnitQty * dUnitCost)Calc); DebugWriteLineIf(iUnitQty > This message WILL appear); DebugWriteLineIf(iUnitQty < This message will NOT appear); DebugAssert(dUnitCost > Message will NOT appear); DebugAssert(dUnitCost < Message will appear since dUnitcost < is false); TextWriterTraceListener tr = new TextWriterTraceListener(SystemConsoleOut); DebugListenersAdd(tr); TextWriterTraceListener tr = new TextWriterTraceListener(SystemIOFileCreateText(Outputtxt)); DebugListenersAdd(tr); DebugWriteLine(The product name is +sProdName); DebugWriteLine(The available units on hand are+iUnitQty); DebugWriteLine(The per unit cost is +dUnitCost); DebugUnindent(); DebugWriteLine(Debug InformationProduct Ending); DebugFlush(); TraceWriteLine(Trace InformationProduct Starting ); TraceIndent(); TraceWriteLine(The product name is +sProdName); TraceWriteLine(The product name is+sProdNameField ); TraceWriteLineIf(iUnitQty > This message WILL appear); TraceAssert(dUnitCost > Message will NOT appear); TraceUnindent(); TraceWriteLine(Trace InformationProduct Ending); TraceFlush(); ConsoleReadLine(); } }

  注意要使此代碼示例發揮作用必須通過將 using SystemDiagnostics; 粘貼為第一行代碼來添加 SystemDiagonstics 名稱空間


From:http://tw.wingwit.com/Article/program/net/201311/13710.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.