Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo Trace.WriteIf (Boolean, String, String)

 

Data di pubblicazione: novembre 2016

Scrive un nome di categoria e un messaggio nei listener di traccia della raccolta Listeners se una condizione è true.

Spazio dei nomi:   System.Diagnostics
Assembly:  System (in System.dll)

[ConditionalAttribute("TRACE")]
public static void WriteIf(
	bool condition,
	string message,
	string category
)

Parametri

condition
Type: System.Boolean

true per fare in modo che venga scritto un messaggio; in caso contrario, false.

message
Type: System.String

Messaggio da scrivere.

category
Type: System.String

Nome di categoria usato per organizzare l'output.

By default, the output is written to an instance of T:System.Diagnostics.DefaultTraceListener.

The category parameter can be used to group output messages.

This method calls the M:System.Diagnostics.TraceListener.Write(System.String) method of the trace listener.

Note per gli implementatori:

You can minimize the performance penalty of instrumenting your application by using If...Then statements instead of using M:System.Diagnostics.Trace.WriteIf(System.Boolean,System.String) statements. The following two code examples send the same debugging message. However, the first example is much faster when tracing is off, because if mySwitch.TraceError evaluates to false you do not call M:System.Diagnostics.Trace.Write(System.String). The second example always calls M:System.Diagnostics.Trace.WriteIf(System.Boolean,System.String), even when mySwitch.TraceError is false and no tracing output is produced. This can result in unnecessary execution of arbitrarily complex code.

First example

if(mySwitch.TraceError) 
    Trace.Write("aNumber = " + aNumber + " out of range");

Second example

Trace.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");

The following example creates a T:System.Diagnostics.TraceSwitch named generalSwitch. This switch is set outside the code sample.

If the switch is set to the T:System.Diagnostics.TraceLevelVerbose, the example outputs the first error message to the P:System.Diagnostics.Trace.Listeners. For information on adding a listener to the P:System.Diagnostics.Trace.Listeners collection, see the T:System.Diagnostics.TraceListenerCollection class.

Then, if the T:System.Diagnostics.TraceLevel is set to Error or higher, the example outputs the second error message on the same line as the first message. The second message is followed by a line terminator.

// Class-level declaration.
 // Create a TraceSwitch.
 static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application");

 static public void MyErrorMethod(Object myObject, String category) {
    // Write the message if the TraceSwitch level is set to Verbose.
    Trace.WriteIf(generalSwitch.TraceVerbose, myObject.ToString() + 
       " is not a valid object for category: ", category);

    // Write a second message if the TraceSwitch level is set to Error or higher.
    Trace.WriteLineIf(generalSwitch.TraceError, " Please use a different category.");
 }

.NET Framework
Disponibile da 1.1
Torna all'inizio
Mostra: