TraceSwitch.Level Właściwość

Definicja

Pobiera lub ustawia poziom śledzenia, który określa komunikaty, na które zezwala przełącznik.

public:
 property System::Diagnostics::TraceLevel Level { System::Diagnostics::TraceLevel get(); void set(System::Diagnostics::TraceLevel value); };
public System.Diagnostics.TraceLevel Level { get; set; }
member this.Level : System.Diagnostics.TraceLevel with get, set
Public Property Level As TraceLevel

Wartość właściwości

TraceLevel Jedna z wartości określających poziom komunikatów dozwolonych przez przełącznik.

Wyjątki

Level jest ustawiona na wartość, która nie jest jedną z TraceLevel wartości.

Przykłady

Poniższy przykład kodu tworzy nowy TraceSwitch element i używa przełącznika w celu określenia, czy wyświetlać komunikaty o błędach. Przełącznik jest tworzony na poziomie klasy. MyMethod Zapisuje pierwszy komunikat o błędzie, jeśli właściwość jest ustawiona Level na TraceLevel.Error wartość lub wyższą. 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( "mySwitch","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("mySwitch", "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("mySwitch", "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 .NET Framework aplikacji, aby ustawić poziom aplikacji TraceSwitch, zmodyfikuj 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 tak, jak w poniższym przykładzie:

<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="0" />  
      <add name="myNewSwitch" value="3" />  
      <remove name="mySwitch" />  
      <clear/>  
    </switches>  
  </system.diagnostics>  
</configuration>  

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

Wartość domyślna Level właściwości to TraceLevel.Off. Natomiast w przypadku aplikacji .NET Framework poziom jest uzyskiwany z pliku konfiguracji, jeśli jest dostępny.

Ustawienie tej właściwości powoduje zaktualizowanie TraceErrorwłaściwości , , TraceWarningTraceInfoi TraceVerbose w celu odzwierciedlenia nowej wartości.

Dotyczy

Zobacz też