다음을 통해 공유


TraceSwitch 생성자

정의

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

오버로드

Name Description
TraceSwitch(String, String)

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

TraceSwitch(String, String, String)

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

TraceSwitch(String, String)

Source:
TraceSwitch.cs
Source:
TraceSwitch.cs
Source:
TraceSwitch.cs
Source:
TraceSwitch.cs
Source:
TraceSwitch.cs

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

public:
 TraceSwitch(System::String ^ displayName, System::String ^ description);
public TraceSwitch(string displayName, string? description);
public TraceSwitch(string displayName, string description);
new System.Diagnostics.TraceSwitch : string * string -> System.Diagnostics.TraceSwitch
Public Sub New (displayName As String, description As String)

매개 변수

displayName
String

사용자 인터페이스에 표시할 이름입니다.

description
String

스위치에 대한 설명입니다.

예제

다음 코드 예제에서는 새로 TraceSwitch 만들고 스위치를 사용하여 오류 메시지를 인쇄할지 여부를 결정합니다. 스위치는 클래스 수준에서 만들어집니다. MyMethod속성 TraceLevel.Error 이 설정되면 Level 첫 번째 오류 메시지를 씁니다. 그러나 MyMethod 두 번째 오류 메시지가 .보다 TraceLevel.Verbose작으면 Level 작성하지 않습니다.

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

설명

.NET Framework 앱의 경우 수준을 TraceSwitch설정하려면 애플리케이션의 이름에 해당하는 구성 파일을 편집합니다. 이 파일에서는 스위치를 추가하고 해당 값을 설정하거나, 스위치를 제거하거나, 애플리케이션에서 이전에 설정한 모든 스위치를 지울 수 있습니다. 구성 파일의 형식은 다음 예제와 같이 지정해야 합니다.

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

텍스트를 사용하여 스위치의 값을 지정할 수도 있습니다. 예를 들어 trueBooleanSwitch 열거형 값을 나타내는 텍스트(예:ErrorTraceSwitch<add name="mySwitch" value="Error" /> 은 .에 해당합니다 <add name="mySwitch" value="1" />.

애플리케이션에서 다음 예제와 같이 동일한 이름을 사용하여 TraceSwitch 구성된 스위치 수준을 사용할 수 있습니다.

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

이 생성자는 새 스위치의 속성을 .로 TraceLevel.Off설정합니다Level. 또는 .NET Framework 앱의 경우 가능한 경우 구성 파일에서 스위치 설정을 가져옵니다.

클래스는 TraceSwitch 스위치를 TraceError테스트 Level 하기 위한 , TraceWarningTraceInfoTraceVerbose 속성을 제공합니다. 이 속성은 Level 스위치의 TraceLevel.를 가져오거나 설정합니다.

메모

성능을 향상시키기 위해 클래스에서 멤버 static 를 만들 TraceSwitch 수 있습니다.

추가 정보

적용 대상

TraceSwitch(String, String, String)

Source:
TraceSwitch.cs
Source:
TraceSwitch.cs
Source:
TraceSwitch.cs
Source:
TraceSwitch.cs
Source:
TraceSwitch.cs

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

public:
 TraceSwitch(System::String ^ displayName, System::String ^ description, System::String ^ defaultSwitchValue);
public TraceSwitch(string displayName, string? description, string defaultSwitchValue);
public TraceSwitch(string displayName, string description, string defaultSwitchValue);
new System.Diagnostics.TraceSwitch : string * string * string -> System.Diagnostics.TraceSwitch
Public Sub New (displayName As String, description As String, defaultSwitchValue As String)

매개 변수

displayName
String

사용자 인터페이스에 표시할 이름입니다.

description
String

스위치에 대한 설명입니다.

defaultSwitchValue
String

스위치의 기본값입니다.

설명

매개 변수는 displayName 속성 값을 DisplayName 설정하는 데 사용되고, description 매개 변수는 속성 값을 Description 설정하는 데 사용되며 defaultSwitchValue , 매개 변수는 필드로 저장되고 첫 번째 참조에서 속성을 초기화하는 Value 데 사용됩니다. TraceSwitch(String, String) 자세한 내용 및 코드 예제는 생성자를 참조하세요.

적용 대상