TraceSwitchとは? わかりやすく解説

TraceSwitch クラス

コードを再コンパイルせずに、トレースデバッグ出力制御する複数レベルスイッチ提供します

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

Public Class TraceSwitch
    Inherits Switch
public class TraceSwitch : Switch
public class TraceSwitch extends Switch
public class TraceSwitch extends
 Switch
解説解説

トレース スイッチ使用すると、メッセージ重要度基づいてメッセージフィルタ処理を行うことができます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定義するには、DisplayNamemySwitch設定し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 以外のコンパイラ使用する場合は、使用するコンパイラドキュメント参照してください

メモメモ

これらのデバッグおよびトレースコンパイラ スイッチは、TraceSwitch クラス単独使用する場合には必要ありません。これらが必要になるのは、Trace クラスまたは Debug クラス条件付きコンパイルされたメソッド組み合わせて使用する場合だけです。

アプリケーション導入詳細については、Debug および Traceトピック参照してくださいトレース スイッチ構成と使用詳細については、「トレース スイッチ」を参照してください

メモメモ

クラスTraceSwitchメンバstatic にすることで、パフォーマンス向上させることができます

使用例使用例

新し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.Object
   System.Diagnostics.Switch
    System.Diagnostics.TraceSwitch
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

TraceSwitch コンストラクタ (String, String, String)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

スイッチ表示名説明、および既定値指定して、TraceSwitch クラス新しインスタンス初期化します。

名前空間: System.Diagnostics
アセンブリ: 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

ユーザー インターフェイス表示する名前。

description

スイッチ説明

defaultSwitchValue

スイッチ既定値

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

TraceSwitch コンストラクタ


TraceSwitch コンストラクタ (String, String)

表示名説明指定して、TraceSwitch クラス新しインスタンス初期化します。

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

解説解説

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メンバstatic にすることで、パフォーマンス向上させることができます

使用例使用例

新し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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

TraceSwitch プロパティ


TraceSwitch メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

TraceSwitch クラス
System.Diagnostics 名前空間
Switch クラス
BooleanSwitch クラス
TraceLevel 列挙
Debug クラス
Trace クラス

TraceSwitch メンバ

コードを再コンパイルせずに、トレースデバッグ出力制御する複数レベルスイッチ提供します

TraceSwitch データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

TraceSwitch クラス
System.Diagnostics 名前空間
Switch クラス
BooleanSwitch クラス
TraceLevel 列挙
Debug クラス
Trace クラス



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「TraceSwitch」の関連用語

TraceSwitchのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TraceSwitchのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS