Suggerisci traduzione
 
Altri utenti hanno suggerito:

progress indicator
Nessun altro suggerimento.
Valuta il contenuto e lascia un commento
Comprimi tutto/Espandi tutto Comprimi tutto
Visualizza contenuto:  affiancatoVisualizza contenuto: affiancato
.NET Framework 4
Trace Switches

Trace switches allow you to enable, disable, and filter tracing output. They are objects that exist in your code and can be configured externally through the .config file. There are three types of trace switches provided in the .NET Framework: the BooleanSwitch class, the TraceSwitch class, and the SourceSwitch class. The BooleanSwitch class acts as a toggle switch, either enabling or disabling a variety of trace statements. The TraceSwitch and SourceSwitch classes allow you to enable a trace switch for a particular tracing level so that the Trace or TraceSource messages specified for that level and all levels below it appear. If you disable the switch, the trace messages will not appear. All these classes derive from the abstract (MustInherit) class Switch, as should any user-developed switches.

Trace switches can be useful for filtering information. For example, you might want to see every tracing message in a data access module, but only error messages in the rest of the application. In that case, you would use one trace switch for the data access module and one switch for the rest of the application. By using the .config file to configure the switches to the appropriate settings, you could control what types of trace message you received. For more information, see How to: Create and Initialize Trace Switches and How to: Configure Trace Switches.

Typically, a deployed application is executed with its switches disabled, so that users need not observe a lot of irrelevant trace messages appearing on a screen or filling up a log file as the application runs. If a problem arises during application execution, you can stop the application, enable the switches, and restart the application. Then the tracing messages will be displayed.

To use a switch you must first create a switch object from a BooleanSwitch class, a TraceSwitch class, or a developer-defined switch class. For more information about creating developer-defined switches, see Switch Class in the .NET Framework reference. Then you set a configuration value that specifies when the switch object is to be used. You then test the setting of the switch object in various Trace (or Debug) tracing methods.

When you use TraceSwitch, there are additional considerations. A TraceSwitch object has four properties that return Boolean values indicating whether the switch is set to at least a particular level:

Levels allow you to limit the amount of tracing information you receive to only that information needed to solve a problem. You specify the level of detail you want in your tracing output by setting and configuring trace switches to the appropriate trace level. You can receive error messages, warning messages, informational messages, verbose tracing messages, or no message at all.

It is entirely up to you to decide what kind of message to associate with each level. Typically, the content of tracing messages depends on what you associate with each level, but you determine the differences between levels. You might want to provide detailed descriptions of a problem at level 3 (Info), for example, but provide only an error reference number at level 1 (Error). It is entirely up to you to decide what scheme works best in your application.

These properties correspond to the values 1 through 4 of the TraceLevel enumeration. The following table lists the levels of the TraceLevel enumeration and their values.

Enumerated value

Integer value

Type of message displayed (or written to a specified output target)

Off

0

None

Error

1

Only error messages

Warning

2

Warning messages and error messages

Info

3

Informational messages, warning messages, and error messages

Verbose

4

Verbose messages, informational messages, warning messages, and error messages

The TraceSwitch properties indicate the maximum trace level for the switch. That is, tracing information is written for the level specified as well as for all lower levels. For example, if TraceInfo is true, then TraceError and TraceWarning are also true but TraceVerbose might well be false.

These properties are read-only. The TraceSwitch object automatically sets them when the TraceLevel property is set. For example:

Visual Basic
Dim myTraceSwitch As New TraceSwitch("SwitchOne", "The first switch")
myTraceSwitch.Level = TraceLevel.Info
' This message box displays true, becuase setting the level to
' TraceLevel.Info sets all lower levels to true as well.
MessageBox.Show(myTraceSwitch.TraceWarning.ToString())
' This messagebox displays false.
MessageBox.Show(myTraceSwitch.TraceVerbose.ToString())
C#
System.Diagnostics.TraceSwitch myTraceSwitch = 
   new System.Diagnostics.TraceSwitch("SwitchOne", "The first switch");
myTraceSwitch.Level = System.Diagnostics.TraceLevel.Info;
// This message box displays true, becuase setting the level to 
// TraceLevel.Info sets all lower levels to true as well.
MessageBox.Show(myTraceSwitch.TraceWarning.ToString());
// This message box displays false.
MessageBox.Show(myTraceSwitch.TraceVerbose.ToString());
J#
System.Diagnostics.TraceSwitch myTraceSwitch =
   new System.Diagnostics.TraceSwitch("SwitchOne", "The first switch");
myTraceSwitch.set_Level(System.Diagnostics.TraceLevel.Info);
// This message box displays true, becuase setting the level to 
// TraceLevel.Info sets all lower levels to true as well.
MessageBox.Show(Convert.ToString(myTraceSwitch.get_TraceWarning()));
// This message box displays false.
MessageBox.Show(Convert.ToString(myTraceSwitch.get_TraceVerbose()));}

In addition to providing BooleanSwitch and TraceSwitch, you can define your own switches by inheriting from the Switch class and overriding the base class methods with customized methods. For more information about creating developer-defined switches, see Switch Class in the .NET Framework reference.

.NET Framework 4
Opzioni di traccia

Le opzioni di traccia consentono di attivare, disabilitare e filtrare l'output di tracciatura. Si tratta di oggetti presenti nel codice che possono essere configurati esternamente tramite il file CONFIG. Esistono tre tipi di opzioni di traccia in .NET Framework, ossia le classi BooleanSwitch, TraceSwitch e SourceSwitch. La classe BooleanSwitch funge da opzione di attivazione o disabilitazione per diverse istruzioni di traccia. Le classi TraceSwitch e SourceSwitch consentono di attivare un'opzione per un particolare livello di tracciatura, in modo che vengano visualizzati i messaggi di traccia Trace o TraceSource specificati per tale livello e per tutti i livelli inferiori. Se si disabilita l'opzione, i messaggi di traccia non verranno visualizzati. Tutte queste classi derivano dalla classe Switch (MustInherit) astratta, come avviene per ogni opzione sviluppata dall'utente.

Le opzioni di traccia possono risultare utili per filtrare informazioni. Può essere ad esempio necessario visualizzare ogni messaggio di tracciatura in un modulo di accesso ai dati, ma solo i messaggi di errore nel resto dell'applicazione. In tal caso, si utilizza un'opzione di traccia per il modulo di accesso ai dati e un'opzione per il resto dell'applicazione. Utilizzando il file CONFIG per configurare le opzioni sulle impostazioni appropriate, è possibile controllare il tipo di messaggi di traccia ricevuti. Per ulteriori informazioni, vedere Procedura: creare e inizializzare opzioni di traccia e Procedura: configurare opzioni di traccia.

In genere un'applicazione distribuita viene eseguita con le opzioni disabilitate, in modo che non vengano visualizzati messaggi di traccia superflui o non sia necessario compilare file di log durante l'esecuzione dell'applicazione. Se sorge un problema durante l'esecuzione dell'applicazione, è possibile arrestare l'applicazione, attivare le opzioni e riavviare l'applicazione. I messaggi di tracciatura verranno in questo modo visualizzati.

Per utilizzare un'opzione è prima necessario creare un oggetto opzione da una classe BooleanSwitch, da una classe TraceSwitch o da una classe di opzione definita dallo sviluppatore. Per ulteriori informazioni sulla creazione di opzioni definite dallo sviluppatore, vedere Classe Switch negli argomenti di riferimento su .NET Framework. Si imposta poi un valore di configurazione che specifica quando occorre utilizzare l'oggetto opzione. Si procede quindi alla verifica dell'impostazione dell'oggetto opzione in diversi metodi di tracciatura Trace o Debug.

Se si utilizza TraceSwitch è necessario procedere a ulteriori considerazioni. Un oggetto TraceSwitch ha quattro proprietà che restituiscono valori Boolean che indicano se l'opzione è impostata su almeno un livello particolare:

I livelli consentono di limitare il quantitativo di informazioni di tracciatura che si riceve unicamente alle informazioni necessarie alla risoluzione di un problema. Si specifica il livello di dettaglio desiderato nell'output di tracciatura impostando e configurando le opzioni di traccia al livello di traccia appropriato. È possibile ricevere messaggi di errore, messaggi di avviso, messaggi informativi, messaggi di tracciatura dettagliati o nessun messaggio.

È possibile decidere quale tipo di messaggi associare a ciascun livello. In genere il contenuto dei messaggi di tracciatura dipende da ciò che viene associato a ciascun livello, ma è possibile stabilire le differenze tra i livelli. È possibile ad esempio fornire descrizioni dettagliate di un problema a livello 3 (Info), ma fornire solo un numero di riferimento per l'errore a livello 1 (Error). È possibile stabilire lo schema più adatto per ogni applicazione.

Queste proprietà corrispondono ai valori da 1 a 4 dell'enumerazione TraceLevel. Nella tabella che segue sono elencati i livelli dell'enumerazione TraceLevel e i relativi valori.

Valore enumerato

Valore integer

Tipo di messaggio visualizzato o scritto in una destinazione di output specificata

Off

0

Nessuno

Error

1

Solo messaggi di errore.

Avviso

2

Messaggi di avviso e messaggi di errore.

Info

3

Messaggi informativi, messaggi di avviso e messaggi di errore.

Verbose

4

Messaggi dettagliati, messaggi informativi, messaggi di avviso e messaggi di errore.

Le proprietà TraceSwitch indicano il livello di traccia massimo per l'opzione. Vale a dire che l'informazione di tracciatura viene scritta per il livello specificato e per tutti i livelli inferiori. Se, ad esempio, TraceInfo è true, anche TraceError e TraceWarning saranno true ma TraceVerbose potrebbe essere false.

Queste proprietà sono in sola lettura. L'oggetto TraceSwitch le imposta automaticamente quando viene impostata la proprietà TraceLevel. Di seguito è riportato un esempio:

Visual Basic
Dim myTraceSwitch As New TraceSwitch("SwitchOne", "The first switch")
myTraceSwitch.Level = TraceLevel.Info
' This message box displays true, becuase setting the level to
' TraceLevel.Info sets all lower levels to true as well.
MessageBox.Show(myTraceSwitch.TraceWarning.ToString())
' This messagebox displays false.
MessageBox.Show(myTraceSwitch.TraceVerbose.ToString())
C#
System.Diagnostics.TraceSwitch myTraceSwitch = 
   new System.Diagnostics.TraceSwitch("SwitchOne", "The first switch");
myTraceSwitch.Level = System.Diagnostics.TraceLevel.Info;
// This message box displays true, becuase setting the level to 
// TraceLevel.Info sets all lower levels to true as well.
MessageBox.Show(myTraceSwitch.TraceWarning.ToString());
// This message box displays false.
MessageBox.Show(myTraceSwitch.TraceVerbose.ToString());
J#
System.Diagnostics.TraceSwitch myTraceSwitch =
   new System.Diagnostics.TraceSwitch("SwitchOne", "The first switch");
myTraceSwitch.set_Level(System.Diagnostics.TraceLevel.Info);
// This message box displays true, becuase setting the level to 
// TraceLevel.Info sets all lower levels to true as well.
MessageBox.Show(Convert.ToString(myTraceSwitch.get_TraceWarning()));
// This message box displays false.
MessageBox.Show(Convert.ToString(myTraceSwitch.get_TraceVerbose()));}

Oltre a fornire BooleanSwitch e TraceSwitch, è possibile definire opzioni personalizzate ereditando dalla classe Switch ed eseguendo l'override dei metodi della classe base con i metodi personalizzati. Per ulteriori informazioni sulla creazione di opzioni definite dallo sviluppatore, vedere Classe Switch negli argomenti di riferimento su .NET Framework.

Contenuti della community   Che cos'è Contenuti della community?
Aggiungi nuovo contenuto RSS  Annotazioni
Processing
© 2012 Microsoft. Tutti i diritti riservati. Condizioni per l'utilizzo | Marchi | Informativa sulla privacy
Page view tracker