WriteLineIf Método (Boolean, String)
Collapse the table of content
Expand the table of content
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

Trace.WriteLineIf (Método) (Boolean, String)

 

Escribe un mensaje a los agentes de escucha 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,
	message As String
)

Parámetros

condition
Type: System.Boolean

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

message
Type: System.String

Mensaje que se va a escribir.

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

Public Shared Sub MyErrorMethod()
    ' Write the message if the TraceSwitch level is set to Error or higher.
    Trace.WriteIf(generalSwitch.TraceError, "My error message. ")

    ' Write a second message if the TraceSwitch level is set to Verbose.
    Trace.WriteLineIf(generalSwitch.TraceVerbose, "My second error message.")
End Sub

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar:
© 2016 Microsoft