TraceSwitch Costruttori

Definizione

Inizializza una nuova istanza della classe TraceSwitch.

Overload

TraceSwitch(String, String)

Inizializza una nuova istanza della classe TraceSwitch utilizzando il nome e la descrizione specificati.

TraceSwitch(String, String, String)

Inizializza una nuova istanza della classe TraceSwitch utilizzando il nome, la descrizione e il valore predefinito specificati per l'opzione.

TraceSwitch(String, String)

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

Inizializza una nuova istanza della classe TraceSwitch utilizzando il nome e la descrizione specificati.

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)

Parametri

displayName
String

Nome da visualizzare in un'interfaccia utente.

description
String

Descrizione dell'opzione.

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( "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

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="1" />  
    </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" />.

Nell'applicazione è possibile usare il livello di opzione configurato creando un TraceSwitch oggetto con lo stesso nome, come illustrato nell'esempio seguente:

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

Questo costruttore imposta la Level proprietà della nuova opzione su TraceLevel.Off. In alternativa, per le app .NET Framework, le impostazioni del commutatore vengono ottenute dal file di configurazione, se disponibile.

La TraceSwitch classe fornisce le TraceErrorproprietà , TraceWarning, TraceInfoe TraceVerbose per testare l'oggetto dell'opzione Level . La Level proprietà ottiene o imposta l'oggetto dell'opzione TraceLevel.

Nota

Per migliorare le prestazioni, è possibile creare TraceSwitch membri static nella classe.

Vedi anche

Si applica a

TraceSwitch(String, String, String)

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

Inizializza una nuova istanza della classe TraceSwitch utilizzando il nome, la descrizione e il valore predefinito specificati per l'opzione.

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)

Parametri

displayName
String

Nome da visualizzare in un'interfaccia utente.

description
String

Descrizione dell'opzione.

defaultSwitchValue
String

Valore predefinito dell'opzione.

Commenti

Il displayName parametro viene utilizzato per impostare il valore della DisplayName proprietà , il description parametro viene utilizzato per impostare il valore della Description proprietà e il defaultSwitchValue parametro viene salvato come campo e utilizzato per inizializzare la Value proprietà sul primo riferimento. Per altre informazioni e un esempio di codice, vedere il TraceSwitch(String, String) costruttore.

Si applica a