Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método Trace.WriteLineIf (Boolean, Object)

 

Publicado: octubre de 2016

Escribe el valor del método ToString del objeto en los agentes de escucha de seguimiento de la colección Listeners si una condición es true.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

<ConditionalAttribute("TRACE")>
Public Shared Sub WriteLineIf (
	condition As Boolean,
	value As Object
)

Parámetros

condition
Type: System.Boolean

true para que se escriba un mensaje; de lo contrario, false.

value
Type: System.Object

Un elemento Object cuyo nombre se envía 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.

Notas para implementadores:

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.
Private Shared generalSwitch As New TraceSwitch("General", "Entire Application")


Public Shared Sub MyErrorMethod(myObject As Object)
    ' 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)
End Sub

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar: