Switch クラス

定義

新しいデバッグ スイッチおよびトレース スイッチを作成するための抽象基本クラスを提供します。

public ref class Switch abstract
public abstract class Switch
type Switch = class
Public MustInherit Class Switch
継承
Switch
派生

次の例では、呼び出し履歴のトレースに使用できる 4 レベルのトレースを使用して新しい Switch クラスを定義する方法を示します。 スイッチを使用すると、メソッドが入力または終了するたびにアプリケーションをインストルメント化してログを記録できます。

最初の例では、スイッチのレベルを設定するために使用する列挙体を作成します。

// The following are possible values for the new switch.
public enum class MethodTracingSwitchLevel
{
    Off = 0,
    EnteringMethod = 1,
    ExitingMethod = 2,
    Both = 3
};
// The following are possible values for the new switch.
public enum MethodTracingSwitchLevel
{
    Off = 0,
    EnteringMethod = 1,
    ExitingMethod = 2,
    Both = 3,
}
' The following are possible values for the new switch.
Public Enum MethodTracingSwitchLevel
    Off = 0
    EnteringMethod = 1
    ExitingMethod = 2
    Both = 3
End Enum 'MethodTracingSwitchLevel

次の例では、新しいスイッチを作成します。 コードは、 Level 新しいスイッチの値を設定するプロパティを実装します。 Level は、新しいスイッチに値を割り当てる保護されたプロパティ SwitchSetting を呼び出します。 この例では、スイッチの割り当てられた値を取得するために、2 つの評価プロパティも実装します。

public ref class MyMethodTracingSwitch: public Switch
{
protected:
    bool outExit;
    bool outEnter;
    MethodTracingSwitchLevel level;

public:
    MyMethodTracingSwitch( String^ displayName, String^ description )
       : Switch( displayName, description )
    {}

    property MethodTracingSwitchLevel Level
    {
        MethodTracingSwitchLevel get()
        {
            return level;
        }

       void set( MethodTracingSwitchLevel value )
       {
           SetSwitchSetting( (int)value );
       }
    }

protected:
    void SetSwitchSetting( int value )
    {
        if ( value < 0 )
        {
            value = 0;
        }

        if ( value > 3 )
        {
            value = 3;
        }

        level = (MethodTracingSwitchLevel)value;
        outEnter = false;
        if ((value == (int)MethodTracingSwitchLevel::EnteringMethod) ||
            (value == (int)MethodTracingSwitchLevel::Both))
        {
            outEnter = true;
        }

        outExit = false;
        if ((value == (int)MethodTracingSwitchLevel::ExitingMethod) ||
            (value == (int)MethodTracingSwitchLevel::Both))
        {
            outExit = true;
        }
    }

public:
    property bool OutputExit
    {
        bool get()
        {
            return outExit;
        }
    }

    property bool OutputEnter
    {
        bool get()
        {
            return outEnter;
        }
    }
};
public class MyMethodTracingSwitch : Switch
{
    protected bool outExit;
    protected bool outEnter;
    protected MethodTracingSwitchLevel level;

    public MyMethodTracingSwitch(string displayName, string description) :
        base(displayName, description)
    {
    }

    public MethodTracingSwitchLevel Level
    {
        get
        {
            return level;
        }
        set
        {
            SetSwitchSetting((int)value);
        }
    }

    protected void SetSwitchSetting(int value)
    {
        if (value < 0)
        {
            value = 0;
        }
        if (value > 3)
        {
            value = 3;
        }

        level = (MethodTracingSwitchLevel)value;

        outEnter = false;
        if ((value == (int)MethodTracingSwitchLevel.EnteringMethod) ||
            (value == (int)MethodTracingSwitchLevel.Both))
        {
            outEnter = true;
        }

        outExit = false;
        if ((value == (int)MethodTracingSwitchLevel.ExitingMethod) ||
            (value == (int)MethodTracingSwitchLevel.Both))
        {
            outExit = true;
        }
    }

    public bool OutputExit
    {
        get
        {
            return outExit;
        }
    }

    public bool OutputEnter
    {
        get
        {
            return outEnter;
        }
    }
}
Public Class MyMethodTracingSwitch
    Inherits Switch
    Protected outExit As Boolean
    Protected outEnter As Boolean
    Protected myLevel As MethodTracingSwitchLevel
    
    Public Sub New(displayName As String, description As String)
        MyBase.New(displayName, description)
    End Sub


    Public Property Level() As MethodTracingSwitchLevel
        Get
            Return myLevel
        End Get
        Set
            SetSwitchSetting(CInt(value))
        End Set
    End Property


    Protected Sub SetSwitchSetting(value As Integer)
        If value < 0 Then
            value = 0
        End If
        If value > 3 Then
            value = 3
        End If

        myLevel = CType(value, MethodTracingSwitchLevel)

        outEnter = False
        If value = CInt(MethodTracingSwitchLevel.EnteringMethod) Or _
            value = CInt(MethodTracingSwitchLevel.Both) Then

            outEnter = True
        End If

        outExit = False
        If value = CInt(MethodTracingSwitchLevel.ExitingMethod) Or _
            value = CInt(MethodTracingSwitchLevel.Both) Then

            outExit = True
        End If
    End Sub


    Public ReadOnly Property OutputExit() As Boolean
        Get
            Return outExit
        End Get
    End Property


    Public ReadOnly Property OutputEnter() As Boolean
        Get
            Return outEnter
        End Get
    End Property
End Class

次の例では、新しいスイッチを作成します Main。 新しいスイッチを作成し、値を割り当てます。 次に、スイッチの設定に応じて、メソッドを入力および終了するためのデバッグ メッセージを出力します。

public ref class Class1
{
private:

    /* Create an instance of MyMethodTracingSwitch.*/
    static MyMethodTracingSwitch^ mySwitch =
        gcnew MyMethodTracingSwitch( "Methods","Trace entering and exiting method" );

public:
    static void main()
    {
        // Add the console listener to see trace messages as console output
        Trace::Listeners->Add(gcnew ConsoleTraceListener(true));
        Debug::AutoFlush = true;

        // Set the switch level to both enter and exit
        mySwitch->Level = MethodTracingSwitchLevel::Both;

        // Write a diagnostic message if the switch is set to entering.
        Debug::WriteLineIf(mySwitch->OutputEnter, "Entering Main");

        // Insert code to handle processing here...

        // Write another diagnostic message if the switch is set to exiting.
        Debug::WriteLineIf(mySwitch->OutputExit, "Exiting Main");
    }
};
public class Class1
{
    /* Create an instance of MyMethodTracingSwitch.*/
    static MyMethodTracingSwitch mySwitch =
        new MyMethodTracingSwitch("Methods", "Trace entering and exiting method");

    public static void Main()
    {
        // Add the console listener to see trace messages as console output
        Trace.Listeners.Add(new ConsoleTraceListener(true));
        Debug.AutoFlush = true;

        // Set the switch level to both enter and exit
        mySwitch.Level = MethodTracingSwitchLevel.Both;

        // Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main");

        // Insert code to handle processing here...

        // Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main");
    }
}
Public Class Class1
    ' Create an instance of MyMethodTracingSwitch.
    Private Shared mySwitch As New _
        MyMethodTracingSwitch("Methods", "Trace entering and exiting method")

    Public Shared Sub Main()
        ' Add the console listener to see trace messages as console output
        Trace.Listeners.Add(New ConsoleTraceListener(True))
        Debug.AutoFlush = True

        ' Set the switch level to both enter and exit
        mySwitch.Level = MethodTracingSwitchLevel.Both

        ' Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main")

        ' Insert code to handle processing here...

        ' Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main")
    End Sub
End Class

注釈

スイッチは、外部設定を使用して実行時にトレースとデバッグの出力を制御するための効率的なメカニズムを提供します。 このクラスは Switch スイッチの既定の動作を実装するため、実行時にスイッチ レベルを変更できます。

このクラスは、およびTraceSwitchクラスのBooleanSwitchSourceSwitch基本クラスです。 これらのスイッチは、ほとんどのデバッグとトレースのニーズを満たします。 トレース スイッチの詳細については、「 トレース スイッチ」を参照してください。

スイッチを使用するには、トレースまたはデバッグを有効にする必要があります。 次の構文はコンパイラ固有です。 C# または Visual Basic 以外のコンパイラを使用する場合は、コンパイラのドキュメントを参照してください。

  • C# でデバッグを有効にするには、コードを /d:DEBUG コンパイルするときにコンパイラ コマンド ラインにフラグを追加するか、ファイルの先頭に追加 #define DEBUG します。 Visual Basicで、コンパイラ コマンド /d:DEBUG=True ラインにフラグを追加します。

  • C# でトレースを有効にするには、コードの /d:TRACE コンパイル時にコンパイラ コマンド ラインにフラグを追加するか、ファイルの先頭に追加 #define TRACE します。 Visual Basicで、コンパイラ コマンド /d:TRACE=True ラインにフラグを追加します。

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

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

この構成セクションの例では、プロパティをBooleanSwitch設定しDisplayName、値をmySwitch設定して a をEnabledtrue定義します。 アプリケーション内で、次のコード例に示すように、同じ名前のスイッチ値を BooleanSwitch 作成することで、構成済みのスイッチ値を使用できます。

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

public:
    static void Main( )
    {
        //...
        Console::WriteLine("Boolean switch {0} configured as {1}",
            boolSwitch->DisplayName, ((Boolean^)boolSwitch->Enabled)->ToString());
        if (boolSwitch->Enabled)
        {
            //...
        }
    }
private static BooleanSwitch boolSwitch = new BooleanSwitch("mySwitch",
    "Switch in config file");

public static void Main()
{
    //...
    Console.WriteLine("Boolean switch {0} configured as {1}",
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString());
    if (boolSwitch.Enabled)
    {
        //...
    }
}

注意 (実装者)

トレース・レベルが必要な場合、またはスイッチ・レベルを設定するためのメカニズムが提供BooleanSwitchされているものとはTraceSwitch異なる場合は、SourceSwitch継承Switchできます。 このクラスから継承する場合は、メソッドを実装する SwitchSetting 必要があります。

コンストラクター

Switch(String, String)

Switch クラスの新しいインスタンスを初期化します。

Switch(String, String, String)

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

プロパティ

Attributes

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

Description

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

DisplayName

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

SwitchSetting

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

Value

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

メソッド

Equals(Object)

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

(継承元 Object)
GetHashCode()

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

(継承元 Object)
GetSupportedAttributes()

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

GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
OnSwitchSettingChanged()

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

OnValueChanged()

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

ToString()

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

(継承元 Object)

適用対象

こちらもご覧ください