次の方法で共有


Visual Basic .NET でトレース クラスとデバッグ クラスを使用する

この記事では、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 クラスを使用してサンプルを作成する

  1. Visual Basic .NET を使用して、 conInfo という名前の新しいコンソール アプリケーション プロジェクトを作成します。 既定では、 Module1 という名前のパブリック モジュールがプロジェクトに追加されます。

  2. 製品に関する情報を含むように変数を初期化するには、次の Dim ステートメントを追加します。

    Dim sProdName As String = "Widget"
    Dim iUnitQty As Integer = 100
    Dim dUnitCost As Decimal = 1.03
    
  3. クラスが生成するメッセージを、 WriteLine メソッドの最初の入力パラメーターとして指定します。 Ctrl + Alt + O キーの組み合わせを押して、[出力] ウィンドウが表示されるようにします。

    Debug.WriteLine("Debug Information-Product Starting ")
    
  4. 読みやすくするには、 Indent メソッドを使用して、出力ウィンドウで後続のメッセージをインデントします。

    Debug.Indent()
    
  5. 選択した変数の内容を表示するには、次のように 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)
    
  6. WriteLine メソッドを使用して、存在するオブジェクトの名前空間とクラス名を表示することもできます。 たとえば、次のコードは、出力ウィンドウに System.Xml.XmlDocument 名前空間を表示します。

    Dim oxml As New System.Xml.XmlDocument()
    Debug.WriteLine(oxml)
    
  7. 出力を整理するには、 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")
    
  8. 出力ウィンドウは、指定された条件が true に評価された場合にのみ、Debug クラスのWriteLineIfメソッドを使用してメッセージを表示できます。 評価される条件は、 WriteLineIf メソッドの最初の入力パラメーターです。 WriteLineIfの 2 番目のパラメーターは、最初のパラメーターの条件が true に評価された場合にのみ表示されるメッセージです。

    Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear")
    Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear")
    
  9. Debug クラスの Assert メソッドを使用して、指定した条件が false に評価された場合にのみ出力ウィンドウにメッセージが表示されるようにします。

    Debug.Assert(dUnitCost > 1, "Message will NOT appear")
    Debug.Assert(dUnitCost < 1, "Message will appear")
    
  10. コンソール ウィンドウ (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)
    
  11. 読みやすくするために、 Unindent メソッドを使用して、 Debug クラスによって生成される後続のメッセージのインデントを削除します。 IndentメソッドとUnindentメソッドを一緒に使用すると、リーダーは出力をグループとして区別できます。

    Debug.Unindent()
    Debug.WriteLine("Debug Information-Product Ending")
    
  12. 各 Listener オブジェクトがそのすべての出力を確実に受信できるようにするには、Debug クラス バッファーのFlush メソッドを呼び出します。

    Debug.Flush()
    

Trace クラスの使用

Trace クラスを使用して、アプリケーションの実行を監視するメッセージを生成することもできます。 TraceクラスとDebug クラスは、出力を生成する同じメソッドのほとんどを共有します。次に示します。

  • WriteLine
  • WriteLineIf
  • Indent
  • Unindent
  • Assert
  • Flush

TraceクラスとDebug クラスは、個別に使用することも、同じアプリケーション内で一緒に使用することもできます。 デバッグ ソリューション構成プロジェクトでは、 TraceDebug の両方の出力がアクティブです。 プロジェクトは、これらの両方のクラスからすべてのリスナー オブジェクトへの出力を生成します。 ただし、リリース ソリューション構成プロジェクトでは、 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()

動作することを確認する

  1. Debug が現在のソリューション構成であることを確認します。

  2. ソリューション エクスプローラー ウィンドウが表示されない場合は、Ctrl + Alt + L キーの組み合わせを押して、このウィンドウを表示します。

  3. conInfoを右クリックし、 Propertiesをクリックします。

  4. conInfo プロパティ ページの左側のウィンドウの Configuration フォルダーの下で、矢印が [デバッグ] をポイントしていることを確認します。

  5. Configuration フォルダーの上にある [構成] ドロップダウン リスト ボックスで、[Active (Debug)] または [Debug] をクリックし、[OK をクリックします。

  6. Ctrl キーを押しながら Alt キーを押しながら O キーを押して、[出力] ウィンドウを表示します。

  7. F5 キーを押してコードを実行します。 [Assertion Failed]\(アサーションに失敗しました\) ダイアログ ボックスが表示されたら、[ Ignore をクリックします。

  8. コンソール ウィンドウで、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
    
  9. コンソール ウィンドウと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 クラスを作成した後、 TextWriterTraceListenerTrace クラスと Debug クラスから出力を受け取ります。 これは、TraceAdd メソッドを使用するか、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)
    

関連情報