Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Trace.WriteIf Method

Writes information about the trace to the trace listeners in the Listeners collection if a condition is true.

Overload List

Writes the value of the object's ToString method to the trace listeners in the Listeners collection if a condition is true.

[Visual Basic] Overloads Public Shared Sub WriteIf(Boolean, Object)
[C#] public static void WriteIf(bool, object);
[C++] public: static void WriteIf(bool, Object*);
[JScript] public static function WriteIf(Boolean, Object);

Writes a message to the trace listeners in the Listeners collection if a condition is true.

[Visual Basic] Overloads Public Shared Sub WriteIf(Boolean, String)
[C#] public static void WriteIf(bool, string);
[C++] public: static void WriteIf(bool, String*);
[JScript] public static function WriteIf(Boolean, String);

Writes a category name and the value of the object's ToString method to the trace listeners in the Listeners collection if a condition is true.

[Visual Basic] Overloads Public Shared Sub WriteIf(Boolean, Object, String)
[C#] public static void WriteIf(bool, object, string);
[C++] public: static void WriteIf(bool, Object*, String*);
[JScript] public static function WriteIf(Boolean, Object, String);

Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.

[Visual Basic] Overloads Public Shared Sub WriteIf(Boolean, String, String)
[C#] public static void WriteIf(bool, string, string);
[C++] public: static void WriteIf(bool, String*, String*);
[JScript] public static function WriteIf(Boolean, String, String);

Example

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

If the switch is set to the TraceLevel Verbose, the example outputs the first error message to the Listeners. For information on adding a listener to the Listeners collection, see the TraceListenerCollection class.

Then, if the 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.

[Visual Basic] 
' Class-level declaration.
' Create a TraceSwitch.
Private Shared generalSwitch As New TraceSwitch("General", "Entire Application")

Public Shared Sub MyErrorMethod(myObject As Object, category As String)
    ' 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.")
End Sub


[C#] 
// 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.");
 }


[C++] 
// Class-level declaration.
 // Create a TraceSwitch.
 static TraceSwitch* generalSwitch = new TraceSwitch(S"General", S"Entire Application");
 
public:
 static void MyErrorMethod(Object* myObject, String* category) {
    // Write the message if the TraceSwitch level is set to Verbose.
    Trace::WriteIf(generalSwitch->TraceVerbose, String::Concat( myObject, S" 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, S" Please use a different category.");
 }


[JScript] 
// Create a TraceSwitch.
var generalSwitch : TraceSwitch = new TraceSwitch("General", "Entire Application")

function MyErrorMethod(myObject : Object, category : String){
  // 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.")
}

See Also

Trace Class | Trace Members | System.Diagnostics Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.