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속성이 이상으로 설정된 TraceLevel.Error 경우 Level 첫 번째 오류 메시지를 씁니다. 그러나 MyMethod 두 번째 오류 메시지가 .보다 TraceLevel.Verbose작으면 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 스위치 수준을 테스트하기 위한 , TraceWarningTraceInfoTraceVerbose 속성을 제공합니다TraceError. 이 속성은 Level 스위치를 TraceLevel가져오거나 설정합니다.

코드에서 TraceSwitch 만들고 수준을 직접 설정하여 특정 코드 섹션을 계측할 수 있습니다.

.NET Framework 앱에서만 애플리케이션 구성 파일을 통해 수준을 TraceSwitch 설정한 다음 애플리케이션에서 구성된 TraceSwitch 수준을 사용할 수도 있습니다. 애플리케이션 구성 파일에서 스위치를 추가하거나 제거하거나, 스위치 값을 설정하거나, 애플리케이션에서 이전에 설정한 모든 스위치를 지울 수 있습니다. 다음 예제와 같이 구성 파일의 형식은 해야 합니다.

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

이 구성 섹션에서는 TraceSwitch DisplayName 열거형 값TraceLevel.ErrormySwitchLevel 해당하는 집합을 1로 설정하고 1로 설정합니다.

참고

텍스트를 사용하여 스위치의 값을 지정할 수도 있습니다. 예를 들어 true BooleanSwitch열거형 값을 나타내는 텍스트(예:Error TraceSwitch<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 컴파일러 명령줄 플래그입니다.

참고

이러한 디버그 하 고 사용 하는 경우 추적 컴파일러 스위치 필요 하지 않습니다는 TraceSwitch 격리의 클래스입니다. 와 함께 필요한 Trace 또는 Debug 메서드는 조건부로 컴파일됩니다.

애플리케이션을 계측 하는 방법은 참조 하세요 DebugTrace입니다. 구성 및 추적 스위치를 사용 하는 방법에 대 한 자세한 내용은 참조 하세요. 추적 스위치합니다.

참고

성능 향상을 위해 할 수 있습니다 TraceSwitch 멤버 static 클래스에서입니다.

생성자

TraceSwitch(String, String)

지정된 표시 이름과 설명을 사용하여 TraceSwitch 클래스의 새 인스턴스를 초기화합니다.

TraceSwitch(String, String, String)

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

속성

Attributes

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

(다음에서 상속됨 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)
ToString()

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

(다음에서 상속됨 Object)

적용 대상

추가 정보