TraceSwitch クラス
アセンブリ: System (system.dll 内)


トレース スイッチを使用すると、メッセージの重要度に基づいてメッセージのフィルタ処理を行うことができます。TraceSwitch クラスは、スイッチのレベルをテストする TraceError、TraceWarning、TraceInfo、および TraceVerbose の各プロパティを提供します。Level プロパティは、スイッチの TraceLevel を取得または設定します。
アプリケーション構成ファイルで TraceSwitch のレベルを設定し、構成した TraceSwitch レベルをアプリケーションで使用できます。または、TraceSwitch をコード内に作成し、レベルを直接設定してコードの特定のセクションをインストルメントすることもできます。
TraceSwitch を構成するには、アプリケーションの名前に対応した構成ファイルを編集します。このファイルでは、スイッチの追加または削除、スイッチの値の設定、アプリケーションで以前設定されたすべてのスイッチのクリアを実行できます。構成ファイルの書式は次の例のようになります。
<configuration> <system.diagnostics> <switches> <add name="mySwitch" value="1" /> </switches> </system.diagnostics> </configuration>
この構成セクションで TraceSwitch を定義するには、DisplayName を mySwitch に設定し、Level を 1 に設定します。これは列挙値 TraceLevel.Error に対応します。アプリケーション内で、構成したスイッチ レベルを使用するには、次の例に示すように TraceSwitch を同じ名前で作成します。
Private Shared appSwitch As New TraceSwitch("mySwitch", _ "Switch in config file") Public Shared Sub Main(ByVal CmdArgs() As String) '... Console.WriteLine("Trace switch {0} configured as {1}", _ appSwitch.DisplayName, appSwitch.Level.ToString()) If appSwitch.TraceError Then '... End If End Sub
private static TraceSwitch appSwitch = new TraceSwitch("mySwitch", "Switch in config file"); public static void Main(string[] args) { //... Console.WriteLine("Trace switch {0} configured as {1}", appSwitch.DisplayName, appSwitch.Level.ToString()); if (appSwitch.TraceError) { //... } }
既定では、スイッチ Level プロパティは、構成ファイルで指定した値を使用して設定されます。TraceSwitch コンストラクタが構成ファイルの初期スイッチ設定を検出できない場合は、新しいスイッチの Level が既定の TraceLevel.Off に設定されます。
スイッチを使用するには、トレースまたはデバッグを有効にする必要があります。次の構文はコンパイラに固有です。C# または Visual Basic 以外のコンパイラを使用する場合は、使用するコンパイラのドキュメントを参照してください。
-
C# でデバッグを有効にするには、コードのコンパイル時に /d:DEBUG フラグをコンパイラのコマンド ラインに追加するか、#define DEBUG をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:DEBUG=True フラグを追加します。
-
C# でトレースを有効にするには、コードのコンパイル時に /d:TRACE フラグをコンパイラのコマンド ラインに追加するか、#define TRACE をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:TRACE=True フラグを追加します。
![]() |
---|
これらのデバッグおよびトレースのコンパイラ スイッチは、TraceSwitch クラスを単独で使用する場合には必要ありません。これらが必要になるのは、Trace クラスまたは Debug クラスの条件付きコンパイルされたメソッドと組み合わせて使用する場合だけです。 |
アプリケーション導入の詳細については、Debug および Trace のトピックを参照してください。トレース スイッチの構成と使用の詳細については、「トレース スイッチ」を参照してください。

新しい TraceSwitch を作成し、スイッチを使用してエラー メッセージを出力するかどうかを決定するコード例を次に示します。スイッチはクラス レベルで作成されます。MyMethod は、Level プロパティが TraceLevel.Error 以上に設定されている場合に最初のエラー メッセージを書き込みます。ただし、MyMethod は、Level が TraceLevel.Verbose 未満の場合は第 2 のエラー メッセージを書き込みません。
' Class-level declaration. ' Create a TraceSwitch to use in the entire application. Private Shared mySwitch As New TraceSwitch("General", "Entire Application") Public Shared Sub MyMethod() ' Write the message if the TraceSwitch level is set to Error or higher. If mySwitch.TraceError Then Console.WriteLine("My error message.") End If ' Write the message if the TraceSwitch level is set to Verbose. If mySwitch.TraceVerbose Then Console.WriteLine("My second error message.") End If End Sub Public Shared Sub Main() ' Run the method that prints error messages based on the switch level. MyMethod() End Sub
//Class-level declaration. /* Create a TraceSwitch to use in the entire application.*/ static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if(mySwitch.TraceError) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(mySwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
// Class-level declaration. /* Create a TraceSwitch to use in the entire application.*/ private: static TraceSwitch^ mySwitch = gcnew TraceSwitch( "General", "Entire Application" ); public: static void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if ( mySwitch->TraceError ) Console::WriteLine( "My error message." ); // Write the message if the TraceSwitch level is set to Verbose. if ( mySwitch->TraceVerbose ) Console::WriteLine( "My second error message." ); } static void main() { // Run the method that prints error messages based on the switch level. MyMethod(); }
// Class-level declaration. /* Create a TraceSwitch to use in the entire application. */ private static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application"); public static void MyMethod() { //Write the message if the TraceSwitch level is set to Error or higher. if (mySwitch.get_TraceError()) { Console.WriteLine("My error message."); } // Write the message if the TraceSwitch level is set to Verbose. if (mySwitch.get_TraceVerbose()) { Console.WriteLine("My second error message."); } } //MyMethod public static void main(String[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); } //main

System.Diagnostics.Switch
System.Diagnostics.TraceSwitch


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TraceSwitch コンストラクタ (String, String, String)
アセンブリ: System (system.dll 内)

Public Sub New ( _ displayName As String, _ description As String, _ defaultSwitchValue As String _ )
Dim displayName As String Dim description As String Dim defaultSwitchValue As String Dim instance As New TraceSwitch(displayName, description, defaultSwitchValue)
public TraceSwitch ( string displayName, string description, string defaultSwitchValue )
public: TraceSwitch ( String^ displayName, String^ description, String^ defaultSwitchValue )
public TraceSwitch ( String displayName, String description, String defaultSwitchValue )
public function TraceSwitch ( displayName : String, description : String, defaultSwitchValue : String )

displayName パラメータは DisplayName プロパティの値の設定、description パラメータは Description プロパティの値の設定に使用され、defaultSwitchValue パラメータはフィールドとして保存されて Value プロパティを初期化するために最初の参照で使用されます。コード例および詳細については、TraceSwitch(String,String) コンストラクタのトピックを参照してください。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TraceSwitch コンストラクタ

名前 | 説明 |
---|---|
TraceSwitch (String, String) | 表示名と説明を指定して、TraceSwitch クラスの新しいインスタンスを初期化します。 |
TraceSwitch (String, String, String) | スイッチの表示名、説明、および既定値を指定して、TraceSwitch クラスの新しいインスタンスを初期化します。 |

TraceSwitch コンストラクタ (String, String)
アセンブリ: System (system.dll 内)

Public Sub New ( _ displayName As String, _ description As String _ )
Dim displayName As String Dim description As String Dim instance As New TraceSwitch(displayName, description)
public TraceSwitch ( string displayName, string description )
public: TraceSwitch ( String^ displayName, String^ description )
public TraceSwitch ( String displayName, String description )
public function TraceSwitch ( displayName : String, description : String )

TraceSwitch のレベルを設定するには、アプリケーション名に対応する構成ファイルを編集します。このファイルでは、スイッチの追加、その値の設定、スイッチの削除、アプリケーションで以前設定されたすべてのスイッチのクリアを実行できます。構成ファイルの書式は次の例のようになります。
<configuration> <system.diagnostics> <switches> <add name="mySwitch" value="0" /> <add name="myNewSwitch" value="3" /> <remove name="mySwitch" /> <clear/> </switches> </system.diagnostics> </configuration>
TraceSwitch コンストラクタが、構成ファイルの初期スイッチ設定を検出できない場合は、新しいスイッチの Level プロパティが TraceLevel.Off に設定されます。
TraceSwitch クラスは、スイッチの Level をテストする TraceError、TraceWarning、TraceInfo、および TraceVerbose の各プロパティを提供します。Level プロパティは、スイッチの TraceLevel を取得または設定します。
![]() |
---|

新しい TraceSwitch を作成し、スイッチを使用してエラー メッセージを出力するかどうかを決定するコード例を次に示します。スイッチはクラス レベルで作成されます。MyMethod は、Level プロパティが TraceLevel.Error 以上に設定されている場合に最初のエラー メッセージを書き込みます。ただし、MyMethod は、Level が TraceLevel.Verbose 未満の場合は第 2 のエラー メッセージを書き込みません。
' Class-level declaration. ' Create a TraceSwitch to use in the entire application. Private Shared mySwitch As New TraceSwitch("General", "Entire Application") Public Shared Sub MyMethod() ' Write the message if the TraceSwitch level is set to Error or higher. If mySwitch.TraceError Then Console.WriteLine("My error message.") End If ' Write the message if the TraceSwitch level is set to Verbose. If mySwitch.TraceVerbose Then Console.WriteLine("My second error message.") End If End Sub Public Shared Sub Main() ' Run the method that prints error messages based on the switch level. MyMethod() End Sub
//Class-level declaration. /* Create a TraceSwitch to use in the entire application.*/ static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if(mySwitch.TraceError) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(mySwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
// Class-level declaration. /* Create a TraceSwitch to use in the entire application.*/ private: static TraceSwitch^ mySwitch = gcnew TraceSwitch( "General", "Entire Application" ); public: static void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if ( mySwitch->TraceError ) Console::WriteLine( "My error message." ); // Write the message if the TraceSwitch level is set to Verbose. if ( mySwitch->TraceVerbose ) Console::WriteLine( "My second error message." ); } static void main() { // Run the method that prints error messages based on the switch level. MyMethod(); }
// Class-level declaration. /* Create a TraceSwitch to use in the entire application. */ private static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application"); public static void MyMethod() { //Write the message if the TraceSwitch level is set to Error or higher. if (mySwitch.get_TraceError()) { Console.WriteLine("My error message."); } // Write the message if the TraceSwitch level is set to Verbose. if (mySwitch.get_TraceVerbose()) { Console.WriteLine("My second error message."); } } //MyMethod public static void main(String[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); } //main

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TraceSwitch プロパティ

名前 | 説明 | |
---|---|---|
![]() | Attributes | アプリケーション構成ファイルに定義されているカスタム スイッチ属性を取得します。 ( Switch から継承されます。) |
![]() | Description | スイッチの説明を取得します。 ( Switch から継承されます。) |
![]() | DisplayName | スイッチを識別するための名前を取得します。 ( Switch から継承されます。) |
![]() | Level | スイッチが許可するメッセージを決定するトレース レベルを取得または設定します。 |
![]() | TraceError | スイッチがエラー処理メッセージを許可するかどうかを示す値を取得します。 |
![]() | TraceInfo | スイッチが情報メッセージを許可するかどうかを示す値を取得します。 |
![]() | TraceVerbose | スイッチがすべてのメッセージを許可するかどうかを示す値を取得します。 |
![]() | TraceWarning | スイッチが警告メッセージを許可するかどうかを示す値を取得します。 |

名前 | 説明 | |
---|---|---|
![]() | SwitchSetting | このスイッチの現在の設定を取得または設定します。 ( Switch から継承されます。) |
![]() | Value | スイッチの値を取得または設定します。 ( Switch から継承されます。) |

TraceSwitch メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | GetSupportedAttributes | スイッチによってサポートされるカスタム属性を取得します。 ( Switch から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
![]() | OnSwitchSettingChanged | オーバーライドされます。 このスイッチのレベルを更新して修正します。 |
![]() | OnValueChanged | オーバーライドされます。 SwitchSetting プロパティを Value プロパティと等価の整数に設定します。 |

TraceSwitch メンバ
コードを再コンパイルせずに、トレースやデバッグの出力を制御する複数レベルのスイッチを提供します。
TraceSwitch データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Attributes | アプリケーション構成ファイルに定義されているカスタム スイッチ属性を取得します。(Switch から継承されます。) |
![]() | Description | スイッチの説明を取得します。(Switch から継承されます。) |
![]() | DisplayName | スイッチを識別するための名前を取得します。(Switch から継承されます。) |
![]() | Level | スイッチが許可するメッセージを決定するトレース レベルを取得または設定します。 |
![]() | TraceError | スイッチがエラー処理メッセージを許可するかどうかを示す値を取得します。 |
![]() | TraceInfo | スイッチが情報メッセージを許可するかどうかを示す値を取得します。 |
![]() | TraceVerbose | スイッチがすべてのメッセージを許可するかどうかを示す値を取得します。 |
![]() | TraceWarning | スイッチが警告メッセージを許可するかどうかを示す値を取得します。 |

名前 | 説明 | |
---|---|---|
![]() | SwitchSetting | このスイッチの現在の設定を取得または設定します。(Switch から継承されます。) |
![]() | Value | スイッチの値を取得または設定します。(Switch から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | GetSupportedAttributes | スイッチによってサポートされるカスタム属性を取得します。 (Switch から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
![]() | OnSwitchSettingChanged | オーバーライドされます。 このスイッチのレベルを更新して修正します。 |
![]() | OnValueChanged | オーバーライドされます。 SwitchSetting プロパティを Value プロパティと等価の整数に設定します。 |

- TraceSwitchのページへのリンク