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 호출합니다. 이 예제에서는 스위치의 할당된 값을 가져오는 두 개의 평가자 속성도 구현합니다.

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 스위치에 대한 기본 동작을 구현하므로 런타임에 스위치 수준을 변경할 수 있습니다.

이 클래스는 , SourceSwitchTraceSwitch 클래스의 BooleanSwitch기본 클래스입니다. 이러한 스위치는 대부분의 디버깅 및 추적 요구 사항을 충족합니다. 추적 스위치에 대한 자세한 내용은 추적 스위치를 참조하세요.

추적 또는 디버깅 스위치를 사용 하도록 설정 해야 합니다. 다음 구문은 특정 컴파일러입니다. 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 하며 Enabled 값으로 설정 true합니다. 애플리케이션 내에서 만들어 구성 된 스위치 값을 사용할 수 있습니다는 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)
    {
        //...
    }
}

구현자 참고

추적 수준이 필요하거나 , 및 에서 제공하는 BooleanSwitchSourceSwitch 스위치 수준과 TraceSwitch다른 스위치 수준을 설정하는 메커니즘이 필요한 경우 에서 Switch상속할 수 있습니다. 이 클래스에서 상속할 때 메서드를 SwitchSetting 구현해야 합니다.

생성자

Switch(String, String)

Switch 클래스의 새 인스턴스를 초기화합니다.

Switch(String, String, String)

Switch 클래스의 새 인스턴스를 초기화하고 스위치의 표시 이름, 설명 및 기본값을 지정합니다.

속성

Attributes

애플리케이션 구성 파일에 정의된 사용자 지정 스위치 특성을 가져옵니다.

DefaultValue

생성자에 할당된 기본값을 가져옵니다.

Description

스위치에 대한 설명을 가져옵니다.

DisplayName

스위치를 식별하는 데 사용되는 이름을 가져옵니다.

SwitchSetting

이 스위치의 현재 설정을 가져오거나 설정합니다.

Value

스위치의 값을 가져오거나 설정합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetSupportedAttributes()

스위치에서 지원하는 사용자 지정 특성을 가져옵니다.

GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
OnSwitchSettingChanged()

SwitchSetting 속성이 변경될 때 호출됩니다.

OnValueChanged()

Value 속성이 변경될 때 호출됩니다.

Refresh()

추적 구성 데이터를 새로 고칩니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

이벤트

Initializing

Switch 초기화해야 할 때 발생합니다.

적용 대상

추가 정보