TraceSwitch Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy TraceSwitch.

Przeciążenia

TraceSwitch(String, String)

Inicjuje TraceSwitch nowe wystąpienie klasy przy użyciu określonej nazwy wyświetlanej i opisu.

TraceSwitch(String, String, String)

Inicjuje nowe wystąpienie klasy przy użyciu określonej nazwy wyświetlanej TraceSwitch , opisu i wartości domyślnej przełącznika.

TraceSwitch(String, String)

Źródło:
TraceSwitch.cs
Źródło:
TraceSwitch.cs
Źródło:
TraceSwitch.cs

Inicjuje TraceSwitch nowe wystąpienie klasy przy użyciu określonej nazwy wyświetlanej i opisu.

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)

Parametry

displayName
String

Nazwa wyświetlana w interfejsie użytkownika.

description
String

Opis przełącznika.

Przykłady

Poniższy przykład kodu tworzy nowy TraceSwitch i używa przełącznika w celu określenia, czy wyświetlać komunikaty o błędach. Przełącznik jest tworzony na poziomie klasy. MyMethodzapisuje pierwszy komunikat o błędzie, jeśli właściwość jest ustawiona na TraceLevel.Error lub nowsząLevel. Nie zapisuje jednak drugiego komunikatu o błędzie, MyMethod jeśli wartość Level jest mniejsza niż TraceLevel.Verbose.

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

Uwagi

W przypadku aplikacji .NET Framework, aby ustawić poziom pliku TraceSwitchkonfiguracji , edytuj plik konfiguracji odpowiadający nazwie aplikacji. W tym pliku można dodać przełącznik i ustawić jego wartość, usunąć przełącznik lub wyczyścić wszystkie przełączniki ustawione wcześniej przez aplikację. Plik konfiguracji powinien być sformatowany w następujący przykład:

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

Możesz również użyć tekstu, aby określić wartość przełącznika. Na przykład true dla BooleanSwitch tekstu lub reprezentującego wartość wyliczenia, na przykład Error dla elementu TraceSwitch. Linia <add name="mySwitch" value="Error" /> jest równoważna .<add name="mySwitch" value="1" />

W aplikacji można użyć skonfigurowanego poziomu przełącznika, tworząc element TraceSwitch o takiej samej nazwie, jak pokazano w poniższym przykładzie:

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

Ten konstruktor ustawia Level właściwość nowego przełącznika na TraceLevel.Off. W przypadku aplikacji .NET Framework ustawienia przełącznika są uzyskiwane z pliku konfiguracji, jeśli są dostępne.

Klasa TraceSwitch udostępnia TraceErrorwłaściwości , TraceWarning, TraceInfoi TraceVerbose do testowania Level przełącznika. Właściwość Level pobiera lub ustawia przełącznik .TraceLevel

Uwaga

Aby poprawić wydajność, możesz tworzyć TraceSwitch elementy członkowskie static w klasie.

Zobacz też

Dotyczy

TraceSwitch(String, String, String)

Źródło:
TraceSwitch.cs
Źródło:
TraceSwitch.cs
Źródło:
TraceSwitch.cs

Inicjuje nowe wystąpienie klasy przy użyciu określonej nazwy wyświetlanej TraceSwitch , opisu i wartości domyślnej przełącznika.

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)

Parametry

displayName
String

Nazwa wyświetlana w interfejsie użytkownika.

description
String

Opis przełącznika.

defaultSwitchValue
String

Wartość domyślna przełącznika.

Uwagi

Parametr displayName służy do ustawiania wartości DisplayName właściwości, description parametr służy do ustawiania wartości Description właściwości, a defaultSwitchValue parametr jest zapisywany jako pole i używany do inicjowania Value właściwości przy pierwszym odwołaniu. TraceSwitch(String, String) Aby uzyskać więcej informacji i przykład kodu, zobacz konstruktor.

Dotyczy