この記事では、Visual Basic .NET で Debug
クラスと Trace
クラスを使用する方法について説明します。
元の製品バージョン: Visual Basic .NET
元の KB 番号: 313417
まとめ
この記事では、 Debug
クラスと Trace
クラスの使用方法について説明します。 これらのクラスは、Microsoft .NET Framework で使用できます。 これらのクラスを使用すると、アプリケーションの開発中または運用環境へのデプロイ後に、アプリケーションのパフォーマンスに関する情報を提供できます。 これらのクラスは、.NET Framework で使用できるインストルメンテーション機能の一部にすぎません。
要件
次の一覧では、必要な推奨ハードウェア、ソフトウェア、ネットワーク インフラストラクチャ、およびサービス パックの概要を示します。
- Windows
- Visual Basic .NET
また、この記事では、プログラムのデバッグについて理解していることを前提としています。
手法の説明
デバッグ クラスを使用してサンプルを作成するセクションの手順では、Debug
クラスを使用してプログラムの実行に関する情報を提供するコンソール アプリケーションを作成する方法を示します。
プログラムの実行時には、 Debug
クラスのメソッドを使用して、監視、誤動作の検出、パフォーマンス測定情報の提供に役立つメッセージを生成できます。 既定では、 Debug
クラスによって生成されるメッセージは、Microsoft Visual Studio 統合開発環境 (IDE) の [出力] ウィンドウに表示されます。
このサンプル コードでは、 WriteLine
メソッドを使用して、行終端記号が続くメッセージを生成します。 このメソッドを使用してメッセージを生成すると、[出力] ウィンドウに各メッセージが個別の行に表示されます。
Debug
クラスの Assert
メソッドを使用すると、指定した条件が false に評価された場合にのみ、出力ウィンドウにメッセージが表示されます。 メッセージは、ユーザーに対するモーダル ダイアログ ボックスにも表示されます。 ダイアログ ボックスには、メッセージ、プロジェクト名、および Debug.Assert
ステートメント番号が含まれます。 ダイアログ ボックスには、次の 3 つのコマンド ボタンも含まれています。
- 中止: アプリケーションの実行が停止します。
- 再試行: アプリケーションがデバッグ モードに入ります。
- 無視: アプリケーションが続行されます。 ユーザーは、アプリケーションを続行する前に、これらのボタンのいずれかをクリックする必要があります。
Debug
クラスから出力ウィンドウ以外の出力先に出力を送信することもできます。 Debug
クラスには、Listener オブジェクトを含む Listeners
という名前のコレクションがあります。 各 Listener オブジェクトは、 Debug
出力を監視し、出力を指定されたターゲットに転送します。 Listeners
コレクション内の各リスナーは、Debug
クラスによって生成されるすべての出力を受け取ります。 リスナー オブジェクトを定義するには、 TextWriterTraceListener
クラスを使用します。 コンストラクターを使用して、 TextWriterTraceListener
クラスのターゲットを指定できます。 次のような出力ターゲットが考えられます。
System.Console.Out
プロパティを使用したコンソール ウィンドウ。System.IO.File.CreateText("FileName.txt"))
ステートメントを使用したテキスト ファイル。
TextWriterTraceListener
オブジェクトを作成した後、Debug
出力を受け取るために、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
メソッドの省略可能な 2 番目の入力パラメーターとしてカテゴリを含めることができます。 カテゴリを指定すると、出力ウィンドウ メッセージの形式は "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, "Field") Debug.WriteLine("Total Cost is" & iUnitQty * dUnitCost, "Calc")
出力ウィンドウは、指定された条件が true に評価された場合にのみ、
Debug
クラスのWriteLineIf
メソッドを使用してメッセージを表示できます。 評価される条件は、WriteLineIf
メソッドの最初の入力パラメーターです。WriteLineIf
の 2 番目のパラメーターは、最初のパラメーターの条件が 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")
コンソール ウィンドウ (
tr1
) と Output.txt (tr2
) という名前のテキスト ファイルのTextWriterTraceListener
オブジェクトを作成し、各オブジェクトを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 オブジェクトがそのすべての出力を確実に受信できるようにするには、
Debug
クラス バッファーのFlush
メソッドを呼び出します。Debug.Flush()
Trace クラスの使用
Trace
クラスを使用して、アプリケーションの実行を監視するメッセージを生成することもできます。 Trace
クラスとDebug
クラスは、出力を生成する同じメソッドのほとんどを共有します。次に示します。
WriteLine
WriteLineIf
Indent
Unindent
Assert
Flush
Trace
クラスとDebug
クラスは、個別に使用することも、同じアプリケーション内で一緒に使用することもできます。 デバッグ ソリューション構成プロジェクトでは、 Trace
と Debug
の両方の出力がアクティブです。 プロジェクトは、これらの両方のクラスからすべてのリスナー オブジェクトへの出力を生成します。 ただし、リリース ソリューション構成プロジェクトでは、 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を右クリックし、 Propertiesをクリックします。
conInfo プロパティ ページの左側のウィンドウの Configuration フォルダーの下で、矢印が [デバッグ] をポイントしていることを確認します。
Configuration フォルダーの上にある [構成] ドロップダウン リスト ボックスで、[Active (Debug)] または [Debug] をクリックし、[OK をクリックします。
Ctrl キーを押しながら Alt キーを押しながら O キーを押して、[出力] ウィンドウを表示します。
F5 キーを押してコードを実行します。 [Assertion Failed]\(アサーションに失敗しました\) ダイアログ ボックスが表示されたら、[ Ignore をクリックします。
コンソール ウィンドウで、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
はTrace
クラスとDebug
クラスから出力を受け取ります。 これは、Trace
のAdd
メソッドを使用するか、Debug
クラスを使用してListeners
クラスにTextWriterTraceListener
を追加するかに関係なく発生します。Trace
クラスとDebug
クラスで同じターゲットの Listener オブジェクトを追加すると、Debug
またはTrace
が出力を生成するかどうかに関係なく、出力の各行が重複します。Dim tr1 As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(tr1) Dim tr2 As New TextWriterTraceListener(System.Console.Out) Trace.Listeners.Add(tr2)