TraceSwitch クラス

定義

コードの再コンパイルなしでトレース出力およびデバッグ出力を制御するための複数レベルのスイッチを提供します。

public ref class TraceSwitch : System::Diagnostics::Switch
public class TraceSwitch : System.Diagnostics.Switch
type TraceSwitch = class
    inherit Switch
Public Class TraceSwitch
Inherits Switch
継承
TraceSwitch

次のコード例では、新しい TraceSwitch を作成し、 スイッチを使用してエラー メッセージを出力するかどうかを決定します。 スイッチはクラス レベルで作成されます。 MyMethod プロパティが 以上に設定されている場合、最初の Level エラー メッセージを TraceLevel.Error 書き込みます。 ただし、 MyMethod が よりTraceLevel.Verbose小さい場合、2 番目のLevelエラー メッセージは書き込まれません。

   // 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.*/
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 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

注釈

トレース スイッチを使用して、メッセージの重要度に基づいてメッセージをフィルター処理できます。 クラスには TraceSwitch 、スイッチのレベルを TraceErrorテストするための、 TraceWarning、、 TraceInfoおよび TraceVerbose の各プロパティが用意されています。 プロパティは Level 、スイッチ TraceLevelの を取得または設定します。

コードで を TraceSwitch 作成し、レベルを直接設定して、コードの特定のセクションをインストルメント化できます。

.NET Framework アプリでのみ、アプリケーション構成ファイルを使用して のTraceSwitchレベルを設定し、アプリケーションで構成されたTraceSwitchレベルを使用することもできます。 アプリケーション構成ファイルでは、スイッチの追加または削除、スイッチの値の設定、またはアプリケーションによって以前に設定されたすべてのスイッチのクリアを行うことができます。 構成ファイルは、次の例のように書式設定する必要があります。

<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="1" />  
    </switches>  
  </system.diagnostics>  
</configuration>  

この構成セクションでは、 を DisplayName に設定し、 を Level 1 にmySwitch設定し、列挙値 TraceLevel.Errorに対応する を定義TraceSwitchします。

Note

テキストを使用して、スイッチの値を指定することもできます。 たとえば、 true の場合BooleanSwitchや、 などのErrorTraceSwitch列挙値を表すテキストです。 <add name="mySwitch" value="Error" /> という行は、<add name="mySwitch" value="1" /> と同じです。

アプリケーションでは、次の例に示すように、同じ名前の を TraceSwitch 作成することで、構成されたスイッチ レベルを使用できます。

private:
    static TraceSwitch^ appSwitch = gcnew TraceSwitch("mySwitch",
        "Switch in config file");

public:
    static void Main(array<String^>^ args)
    {
        //...
        Console::WriteLine("Trace switch {0} configured as {1}",
        appSwitch->DisplayName, appSwitch->Level.ToString());
        if (appSwitch->TraceError)
        {
            //...
        }
    }
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)
    {
        //...
    }
}
Private Shared appSwitch As new TraceSwitch("mySwitch", _
    "Switch in config file")

Public Shared Sub Main(args As String())
    '...
    Console.WriteLine("Trace switch {0} configured as {1}",
    appSwitch.DisplayName, appSwitch.Level.ToString())
    If appSwitch.TraceError = True  Then
        '...
    End If
End Sub

.NET Core および .NET 5 以降のアプリでは、 Level 新しいスイッチの 既定値は です TraceLevel.Off

.NET Framework アプリでは、switch 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 コンパイラコマンド ラインに追加します。

Note

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

アプリケーションのインストルメント化の詳細については、 と Traceを参照してくださいDebug。 トレース スイッチの構成と使用の詳細については、「トレース スイッチ」を参照してください。

Note

パフォーマンスを向上させるために、クラスのメンバーstaticを作成TraceSwitchできます。

コンストラクター

TraceSwitch(String, String)

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

TraceSwitch(String, String, String)

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

プロパティ

Attributes

アプリケーション構成ファイルに定義されているカスタム スイッチ属性を取得します。

(継承元 Switch)
DefaultValue

コンストラクターで割り当てられた既定値を取得します。

(継承元 Switch)
Description

スイッチの説明を取得します。

(継承元 Switch)
DisplayName

スイッチを識別するための名前を取得します。

(継承元 Switch)
Level

スイッチが許可するメッセージを決定するトレース レベルを取得または設定します。

SwitchSetting

このスイッチの現在の設定を取得または設定します。

(継承元 Switch)
TraceError

スイッチがエラー処理メッセージを許可するかどうかを示す値を取得します。

TraceInfo

スイッチが情報メッセージを許可するかどうかを示す値を取得します。

TraceVerbose

スイッチがすべてのメッセージを許可するかどうかを示す値を取得します。

TraceWarning

スイッチが警告メッセージを許可するかどうかを示す値を取得します。

Value

スイッチの値を取得または設定します。

(継承元 Switch)

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetSupportedAttributes()

スイッチによってサポートされるカスタム属性を取得します。

(継承元 Switch)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
OnSwitchSettingChanged()

このスイッチのレベルを更新して修正します。

OnValueChanged()

SwitchSetting プロパティを Value プロパティと等価の整数に設定します。

OnValueChanged()

Value プロパティが変更されると発生します。

(継承元 Switch)
Refresh()

トレース構成データを更新します。

(継承元 Switch)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください