訓練
模組
使用 Visual Studio 偵錯工具對 .NET 應用程式進行互動式偵錯 - Training
了解如何使用 Visual Studio 有效率地對 .NET 應用程式進行偵錯,以便快速修正錯誤 (Bug)。 使用 Visual Studio 內的互動式偵錯工具來分析並修正您的 C# 應用程式。
本文提供如何在Visual Basic .NET 中使用 Debug
和 Trace
類別的相關信息。
原始產品版本: Visual Basic .NET
原始 KB 編號: 313417
本文示範如何使用 Debug
和 Trace
類別。 這些類別可在 .NET Framework Microsoft取得。 您可以使用這些類別,在應用程式開發期間或部署至生產環境之後,提供應用程式效能的相關信息。 這些類別只是 .NET Framework 中可用的檢測功能的一部分。
下列清單概述您需要的建議硬體、軟體、網路基礎結構和 Service Pack:
本文也假設您已熟悉程序偵錯。
使用Debug類別建立範例一節中的步驟示範如何建立使用 Debug
類別來提供程式執行相關信息的控制台應用程式。
當程式執行時,您可以使用 類別的方法 Debug
來產生有助於監視、偵測故障或提供效能測量資訊的訊息。 根據預設,類別 Debug
產生的訊息會出現在 Microsoft Visual Studio 集成開發環境 (IDE) 的 [輸出] 視窗中。
範例程式代碼會 WriteLine
使用 方法來產生訊息,後面接著行終止符。 當您使用此方法來產生訊息時,每個訊息會出現在 [輸出] 視窗中的個別行上。
如果您使用 Assert
類別的 Debug
方法,只有當指定的條件評估為 false 時,[輸出] 視窗才會顯示訊息。 訊息也會出現在使用者的強制回應對話框中。 對話框包含訊息、專案名稱和 Debug.Assert
語句編號。 對話框也包含三個命令按鈕:
您也可以將 類別的 Debug
輸出導向至 [輸出] 視窗以外的目的地。 類別 Debug
具有名為 Listeners
的集合,其中包含 Listener 物件。 每個接聽程式物件都會 Debug
監視輸出,並將輸出導向至指定的目標。 集合中的每個 Listeners
接聽程式都會接收類別所產生的任何輸出 Debug
。 使用類別 TextWriterTraceListener
來定義 Listener 物件。 您可以透過其建構函式指定 TextWriterTraceListener
類別的目標。 某些可能的輸出目標包括:
System.Console.Out
主控台視窗。System.IO.File.CreateText("FileName.txt"))
文字檔。建立 TextWriterTraceListener
物件之後,您必須將 物件新增至 Debug.Listeners
集合以接收 Debug
輸出。
使用 Visual Basic .NET 建立名為 conInfo 的新控制台應用程式專案。 根據預設,名為 Module1
的公用模組會新增至專案。
若要初始化變數以包含產品的相關信息,請新增下列 Dim
語句:
Dim sProdName As String = "Widget"
Dim iUnitQty As Integer = 100
Dim dUnitCost As Decimal = 1.03
指定類別產生的訊息做為 方法的第一個輸入參數 WriteLine
。 按 CTRL+ALT+O 按鍵組合,以確保 [輸出] 視窗可見。
Debug.WriteLine("Debug Information-Product Starting ")
如需可讀性,請使用 Indent
方法在 [輸出] 視窗中縮排後續訊息:
Debug.Indent()
若要顯示所選變數的內容,請使用 WriteLine
方法,如下所示:
Debug.WriteLine("The product name is " & sProdName)
Debug.WriteLine("The available units on hand are " & iUnitQty)
Debug.WriteLine("The per unit cost is " & dUnitCost)
您也可以使用 WriteLine
方法來顯示現有物件的命名空間和類別名稱。 例如,下列程式代碼會在 [輸出] 視窗中顯示 System.Xml.XmlDocument
命名空間:
Dim oxml As New System.Xml.XmlDocument()
Debug.WriteLine(oxml)
若要組織輸出,您可以將類別納入為方法的 WriteLine
選擇性第二個輸入參數。 如果您指定類別,[輸出] 視窗訊息的格式為 “category: message”。例如,下列程序代碼的第一行會在 [輸出] 視窗中顯示 「字段:產品名稱為 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, "Field")
Debug.WriteLine("Total Cost is" & iUnitQty * dUnitCost, "Calc")
只有在指定的條件使用 WriteLineIf
類別的 Debug
方法評估為 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")
TextWriterTraceListener
針對主控台視窗 (tr1
) 和名為 Output.txt 的tr2
文字檔建立 物件,然後將每個物件新增至Debug
Listeners
集合:
Dim tr1 As New TextWriterTraceListener(System.Console.Out)
Debug.Listeners.Add(tr1)
Dim tr2 As 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
類別緩衝區的 方法:
Debug.Flush()
您也可以使用 類別 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()
請確定 [ 偵錯 ] 是目前的解決方案組態。
如果看不到 方案總管 視窗,請按 CTRL+ALT+L 鍵組合以顯示此視窗。
以滑鼠右鍵按兩下 conInfo,然後按兩下 [ 屬性]。
在 conInfo 屬性頁的左窗格中,於 [組態] 資料夾底下,確定箭號指向 [偵錯]。
在 [組態] 資料夾上方的 [組態] 下拉式清單中,按兩下 [作用中][偵錯] 或 [偵錯],然後按兩下 [確定]。
按 CTRL+ALT+O 以顯示 [輸出] 視窗。
按 F5 鍵以執行程式碼。 出現 [判斷提示失敗] 對話框時,按兩下 [ 忽略]。
在 [主控台] 視窗中,按 ENTER 鍵。 程式應該會完成,而 [輸出] 視窗應該會顯示下列輸出:
Debug Information-Product Starting
The product name is Widget
The available units on hand are 100
The per unit cost is 1.03
System.Xml.XmlDocument
Field: The product name is Widget
Field: The units on hand are 100
Field: The per unit cost is 1.03
Calc: Total cost is 103
This message WILL appear
---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
Message will appear
---- Assert Long Message ----
at Module1.Main() C:\Documents and Settings\Administrator\My
Documents\Visual Studio Projects\conInfo\Module1.vb(29)
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檔案應該會顯示下列輸出:
(The Output.txt file is located in the same directory as the conInfo
executable, conInfo.exe. Normally this is the \bin folder of where the
project source has been stored. By default that would be C:\Documents and
Settings\User login\My Documents\Visual Studio Projects\conInfo\bin)
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
Module Module1
Sub Main()
Dim sProdName As String = "Widget"
Dim iUnitQty As Integer = 100
Dim dUnitCost As Decimal = 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)
Debug.WriteLine("The per unit cost is " & dUnitCost)
Dim oxml As 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, "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")
Dim tr1 As New TextWriter`Trace`Listener(System.Console.Out)
Debug.Listeners.Add(tr1)
Dim tr2 As 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()
End Sub
End Module
如果方案組態類型為 Release,則會 Debug
忽略類別輸出。
TextWriterTraceListener
建立特定目標的類別之後,TextWriterTraceListener
接收和類別的Debug
輸出Trace
。 不論您使用 Add
的方法 Trace
或 Debug
類別來新增 TextWriterTraceListener
至 Listeners
類別,都會發生此情況。
如果您在和類別中Trace
新增相同目標的 Listener 物件,則不論是否Debug
或Trace
產生輸出,每個輸出行都會Debug
重複。
Dim tr1 As New TextWriterTraceListener(System.Console.Out)
Debug.Listeners.Add(tr1)
Dim tr2 As New TextWriterTraceListener(System.Console.Out)
Trace.Listeners.Add(tr2)
訓練
模組
使用 Visual Studio 偵錯工具對 .NET 應用程式進行互動式偵錯 - Training
了解如何使用 Visual Studio 有效率地對 .NET 應用程式進行偵錯,以便快速修正錯誤 (Bug)。 使用 Visual Studio 內的互動式偵錯工具來分析並修正您的 C# 應用程式。
文件
Me、My、MyBase 及 MyClass - Visual Basic
深入了解:Visual Basic 中的 Me、My、MyBase 和 MyClass
深入瞭解:自動實作的屬性 (Visual Basic)
深入了解:使用陳述式 (Visual Basic)