この記事では、Visual C# でトレースおよびデバッグする方法について説明し、関連情報を説明するいくつかのサンプル手順を示します。
元の製品バージョン: Visual C#
元の KB 番号: 815788
まとめ
この記事の Microsoft Visual Basic .NET バージョンについては、「 Visual Basic .NET でトレース クラスとデバッグ クラスを使用するを参照してください。
この記事では、.NET Framework クラス ライブラリ名前空間システムについて説明します。 診断と、 Debug クラスと Trace クラスの使用方法について説明します。 これらのクラスは、.NET Framework で使用できます。 これらのクラスを使用すると、アプリケーションの開発中または運用環境へのデプロイ後に、アプリケーションのパフォーマンスに関する情報を提供できます。 これらのクラスは、.NET Framework で使用できるインストルメンテーション機能の一部にすぎません。
要件
次の一覧では、必要な推奨ハードウェア、ソフトウェア、ネットワーク インフラストラクチャ、およびサービス パックの概要を示します。
- Microsoft Windows
- Microsoft Visual C#
また、この記事では、プログラムのデバッグについて理解していることを前提としています。
手法の説明
「 デバッグ クラスを使用してサンプルを作成する 」セクションの手順では、 Debug クラスを使用してプログラムの実行に関する情報を提供するコンソール アプリケーションを作成する方法を示します。
プログラムの実行時には、 Debug クラスのメソッドを使用して、プログラム実行シーケンスの監視、誤動作の検出、またはパフォーマンス測定情報の提供に役立つメッセージを生成できます。 既定では、 Debug クラスによって生成されるメッセージは、Visual Studio 統合開発環境 (IDE) の [出力] ウィンドウに表示されます。
このサンプル コードでは、 WriteLine メソッドを使用して、行終端記号が続くメッセージを生成します。 このメソッドを使用してメッセージを生成すると、[出力] ウィンドウに各メッセージが個別の行に表示されます。
Debug クラスの Assert メソッドを使用すると、指定した条件が false に評価された場合にのみ、出力ウィンドウにメッセージが表示されます。 メッセージは、ユーザーに対するモーダル ダイアログ ボックスにも表示されます。 ダイアログ ボックスには、メッセージ、プロジェクト名、および Debugが含まれます。 Assert ステートメント番号。 ダイアログ ボックスには、次の 3 つのコマンド ボタンも含まれています。
Abort: アプリケーションの実行が停止します。
Retry: アプリケーションがデバッグ モードに入ります。
Ignore: アプリケーションが続行されます。 ユーザーは、アプリケーションを続行する前に、これらのボタンのいずれかをクリックする必要があります。
Debug クラスから出力ウィンドウ以外の出力先に出力を送信することもできます。 Debug クラスには、Listener オブジェクトを含む Listeners コレクションがあります。
各 Listener オブジェクトは、 Debug 出力を監視し、出力を指定されたターゲットに転送します。
Listener コレクション内の各リスナーは、 Debug クラスによって生成されるすべての出力を受け取ります。 TextWriterTraceListener クラスを使用して、Listenerオブジェクトを定義します。 コンストラクターを使用して、 TextWriterTraceListener クラスのターゲットを指定できます。
次のような出力ターゲットが考えられます。
System.Console.Outプロパティを使用したコンソール ウィンドウ。System.IO.File.CreateText("FileName.txt")ステートメントを使用したテキスト (.txt) ファイル。TextWriterTraceListenerオブジェクトを作成したら、そのオブジェクトをDebugに追加する必要があります。Debug出力を受け取るリスナー コレクション。
デバッグ クラスを使用してサンプルを作成する
Visual Studio または Visual C# Express Edition を起動します。
conInfo という名前の新しい Visual C# コンソール アプリケーション プロジェクトを作成します。 Class1 は Visual Studio .NET で作成されます。 Program.csは Visual Studio 2005 で作成されます。
Class1 または Program.cs の先頭に次の名前空間を追加します。
using System.Diagnostics;製品に関する情報を含むように変数を初期化するには、Main メソッドに次の宣言ステートメントを追加します。
string sProdName = "Widget"; int iUnitQty = 100; double dUnitCost = 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.ToString()); Debug.WriteLine("The per unit cost is " + dUnitCost.ToString());WriteLineメソッドを使用して、存在するオブジェクトの名前空間とクラス名を表示することもできます。 たとえば、次のコードは、出力ウィンドウにSystem.Xml.XmlDocument名前空間を表示します。System.Xml.XmlDocument oxml = 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.ToString(),"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 since dUnitcost < 1 is false");コンソール ウィンドウ (tr1) と Output.txt (tr2) という名前のテキスト ファイルの
TextWriterTraceListenerオブジェクトを作成し、各オブジェクトをデバッグ リスナー コレクションに追加します。TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(tr1); TextWriterTraceListener tr2 = 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
- インデントする
- インデント
- Assert
- フラッシュ
TraceクラスとDebug クラスは、個別に使用することも、同じアプリケーション内で一緒に使用することもできます。 デバッグ ソリューション構成プロジェクトでは、 Trace と Debug の両方の出力がアクティブです。 プロジェクトは、これらの両方のクラスからすべての 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();
動作することを確認する
デバッグが現在のソリューション構成であることを確認します。
ソリューション エクスプローラー ウィンドウが表示されない場合は、CTRL + Alt + L キーの組み合わせを押して、このウィンドウを表示します。
conInfoを右クリックし、 Propertiesをクリックします。
conInfo プロパティ ページの左側のウィンドウの Configuration フォルダーの下で、矢印が Debugging を指していることを確認します。
Note
Visual C# 2005 および Visual C# 2005 Express Edition で、conInfo ページの Debug をクリックします。
Configuration フォルダーの上にある Configuration ドロップダウン リスト ボックスで、[Active (Debug)] または [Debug] をクリックし、OK をクリックします。 Visual C# 2005 および Visual C# 2005 Express Edition で、Debug ページの Configuration ドロップダウン リスト ボックスで Active (Debug) または Debug をクリックし、File メニューの Save をクリックします。
CTRL + Alt + O キーを押して、Output ウィンドウを表示します。
F5 キーを押してコードを実行します。 [ Assertion Failed ] ダイアログ ボックスが表示されたら、[ Ignore をクリックします。
Console ウィンドウで、ENTER キーを押します。 プログラムが完了し、 Output ウィンドウに次のような出力が表示されます。
Debug Information-Product Starting The product name is Widget The available units on hand are100 The per unit cost is 1.03 System.Xml.XmlDocument Field: The product name is Widget Field: The units on hand are100 Field: The per unit cost is1.03 Calc: Total Cost is 103 This message WILL appear ---- DEBUG ASSERTION FAILED ---- ---- Assert Short Message ---- Message will appear since dUnitcost < 1 is false ---- Assert Long Message ---- at Class1.Main(String[] args) <%Path%>\class1.cs(34) The product name is Widget The available units on hand are100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name isWidget This message WILL appear Trace Information-Product EndingConsole ウィンドウとOutput.txt ファイルには、次の出力が表示されます。
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
Note
Output.txt ファイルは、conInfo 実行可能ファイル (conInfo.exe) と同じディレクトリにあります。 通常、これはプロジェクト ソースが格納されている \bin フォルダーです。 既定では、これは C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin です。 Visual C# 2005 および Visual C# 2005 Express Edition では、 Output.txt ファイルはフォルダー C:\Documents and Settings\User login\My Documents\Visual Studio 2005\Projects\conInfo\conInfo\bin\Debugにあります。
完全なコード一覧
using System;
using System.Diagnostics;
class Class1
{
[STAThread]
static void Main(string[] args)
{
string sProdName = "Widget";
int iUnitQty = 100;
double dUnitCost = 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.ToString());
Debug.WriteLine("The per unit cost is "+ dUnitCost.ToString());
System.Xml.XmlDocument oxml = 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.ToString(),"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 since dUnitcost < 1 is false");
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(tr1);
TextWriterTraceListener tr2 = 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();
}
}
トラブルシューティング
ソリューション構成の種類が Release の場合、
Debugクラスの出力は無視されます。特定のターゲットの
TextWriterTraceListenerクラスを作成した後、TextWriterTraceListenerはTraceクラスとDebugクラスから出力を受け取ります。 これは、TraceのAddメソッドを使用するか、Debugクラスを使用してListenersクラスにTextWriterTraceListenerを追加するかに関係なく発生します。TraceクラスとDebugクラスの同じターゲットに対してListenersオブジェクトを追加すると、DebugまたはTraceが出力を生成するかどうかに関係なく、出力の各行が重複します。TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(myWriter); TextWriterTraceListener myCreator = new TextWriterTraceListener(System.Console.Out); Trace.Listeners.Add(myCreator);