TraceSwitch.Level Proprietà

Definizione

Ottiene o imposta il livello di traccia che determina i messaggi consentiti dall'opzione.

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

Valore della proprietà

Uno dei TraceLevel valori che specifica il livello di messaggi consentiti dall'opzione.

Eccezioni

Level è impostata su un valore diverso dai valori di TraceLevel.

Esempio

Nell'esempio di codice seguente viene creato un nuovo TraceSwitch oggetto e viene utilizzata l'opzione per determinare se stampare i messaggi di errore. L'opzione viene creata a livello di classe. MyMethod scrive il primo messaggio di errore se la Level proprietà è impostata su TraceLevel.Error o superiore. Tuttavia, MyMethod non scrive il secondo messaggio di errore se è Level minore di 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

Commenti

Per le app .NET Framework, per impostare il livello di , modificare il file di TraceSwitchconfigurazione corrispondente al nome dell'applicazione. In questo file è possibile aggiungere un'opzione e impostarne il valore, rimuovere un'opzione o cancellare tutte le opzioni precedentemente impostate dall'applicazione. Il file di configurazione deve essere formattato come nell'esempio seguente:

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

È anche possibile usare il testo per specificare il valore di un'opzione. Ad esempio, true per un BooleanSwitch oggetto o il testo che rappresenta un valore di enumerazione, ad esempio Error per un oggetto TraceSwitch. La riga <add name="mySwitch" value="Error" /> equivale a <add name="mySwitch" value="1" />.

Il valore predefinito della Level proprietà è TraceLevel.Off. In alternativa, per le app .NET Framework, il livello viene ottenuto dal file di configurazione, se disponibile.

L'impostazione di questa proprietà aggiorna le TraceErrorproprietà , TraceWarning, TraceInfoe TraceVerbose per riflettere il nuovo valore.

Si applica a

Vedi anche