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.WriteLineIf (Boolean, Object)

 

Data di pubblicazione: ottobre 2016

Scrive il valore del metodo ToString dell'oggetto nei listener di traccia nella raccolta Listeners se una condizione è true.

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

[ConditionalAttribute("TRACE")]
public static void WriteLineIf(
	bool condition,
	object value
)

Parametri

condition
Type: System.Boolean

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

value
Type: System.Object

Oggetto Object il cui nome viene inviato a Listeners.

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

This method calls the M:System.Diagnostics.TraceListener.WriteLine(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.WriteLineIf(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.WriteLine(System.String). The second example always calls M:System.Diagnostics.Trace.WriteLineIf(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.WriteLine("aNumber = " + aNumber + " out of range");

Second example

Trace.WriteLineIf(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.TraceLevelError or higher, 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 Verbose, the example outputs the name of the object 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) {
    // Write the message if the TraceSwitch level is set to Error or higher.
    Trace.WriteIf(generalSwitch.TraceError, "Invalid object. ");

    // Write a second message if the TraceSwitch level is set to Verbose.
    Trace.WriteLineIf(generalSwitch.TraceVerbose, myObject);
 }

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