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.Listeners 受け取る Debug 必要があります。

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. 出力を整理するには、 メソッドの省略可能な 2 番目の入力パラメーターとしてカテゴリを 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")
    
  8. [出力] ウィンドウでは、 クラスの メソッドを使用 WriteLineIf して、指定された条件が true と評価された場合にのみメッセージを Debug 表示できます。 評価される条件は、 メソッドの最初の WriteLineIf 入力パラメーターです。 の 2 番目の WriteLineIf パラメーターは、最初のパラメーターの条件が true と評価された場合にのみ表示されるメッセージです。

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

    Debug.Assert(dUnitCost > 1, "Message will NOT appear")
    Debug.Assert(dUnitCost < 1, "Message will appear")
    
  10. TextWriterTraceListenerコンソール ウィンドウ (tr1) と Output.txttr2() という名前のテキスト ファイルのオブジェクトを作成し、各オブジェクトをコレクションにDebugListeners追加します。

    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 削除します。 メソッドと メソッドを一緒にUnindent使用Indentすると、リーダーは出力をグループとして区別できます。

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

    Debug.Flush()
    

Trace クラスの使用

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

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

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

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

  3. conInfo を右クリックし、[プロパティ] をクリックします。

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

  5. [構成] フォルダーの上にある [構成] ドロップダウン リスト ボックスで、[アクティブ (デバッグ)] または [デバッグ] をクリックし、[OK] をクリックします

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

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

  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 作成した後、 TextWriterTraceListener クラスと クラスから Trace 出力を Debug 受け取ります。 これは、 または クラスのメソッドを使用してAddクラスにDebug追加TextWriterTraceListenerするかどうかに関係なく発生しますListenersTrace

  • と クラス内の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)
    

関連情報