Switch 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
새 디버깅 및 추적 스위치를 만드는 추상 기본 클래스를 제공합니다.
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 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 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 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 런타임에 스위치 수준을 변경할 수 있도록 스위치에 대한 기본 동작을 구현합니다.
이 클래스는 , BooleanSwitch및 SourceSwitch 클래스의 TraceSwitch기본 클래스입니다. 이러한 스위치는 대부분의 디버깅 및 추적 요구 사항을 충족합니다. 추적 스위치에 대한 자세한 내용은 추적 스위치를 참조하세요.
스위치를 사용하려면 추적 또는 디버깅을 사용하도록 설정해야 합니다. 다음 구문은 컴파일러 관련입니다. 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 속성이 설정된 mySwitch 값과 Enabled 값을 DisplayName 정의합니다true. 애플리케이션 내에서 다음 코드 예제와 같이 동일한 이름을 사용하여 구성된 스위치 값을 BooleanSwitch 사용할 수 있습니다.
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)
{
//...
}
}
구현자 참고
추적 수준 또는 스위치 수준을 설정 BooleanSwitchSourceSwitchTraceSwitch하기 위한 메커니즘이 필요한 경우 다음에서 Switch상속할 수 있습니다. 이 클래스에서 상속할 때 메서드를 SwitchSetting 구현해야 합니다.
생성자
| Name | Description |
|---|---|
| Switch(String, String, String) |
스위치의 표시 이름, 설명 및 기본값을 지정하여 클래스의 Switch 새 인스턴스를 초기화합니다. |
| Switch(String, String) |
Switch 클래스의 새 인스턴스를 초기화합니다. |
속성
| Name | Description |
|---|---|
| Attributes |
애플리케이션 구성 파일에 정의된 사용자 지정 스위치 특성을 가져옵니다. |
| DefaultValue |
생성자에 할당된 기본값을 가져옵니다. |
| Description |
스위치에 대한 설명을 가져옵니다. |
| DisplayName |
스위치를 식별하는 데 사용되는 이름을 가져옵니다. |
| SwitchSetting |
이 스위치의 현재 설정을 가져오거나 설정합니다. |
| Value |
스위치의 값을 가져오거나 설정합니다. |
메서드
| Name | Description |
|---|---|
| Equals(Object) |
지정된 개체가 현재 개체와 같은지 여부를 확인합니다. (다음에서 상속됨 Object) |
| GetHashCode() |
기본 해시 함수로 사용됩니다. (다음에서 상속됨 Object) |
| GetSupportedAttributes() |
스위치에서 지원하는 사용자 지정 특성을 가져옵니다. |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| OnSwitchSettingChanged() |
속성이 SwitchSetting 변경될 때 호출됩니다. |
| OnValueChanged() |
속성이 Value 변경될 때 호출됩니다. |
| Refresh() |
추적 구성 데이터를 새로 고칩니다. |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
이벤트
| Name | Description |
|---|---|
| Initializing |
초기화해야 할 때 Switch 발생합니다. |