Trace Class
 

Provides a set of methods and properties that help you trace the execution of your code. This class cannot be inherited.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

Inheritance Hierarchy
SystemObject
  System.DiagnosticsTrace

Syntax
public sealed class Trace
public ref class Trace sealed 
[<Sealed>]
type Trace = class end
Public NotInheritable Class Trace
Properties
NameDescription
System_CAPS_pubproperty System_CAPS_static

Gets or sets whether Flush should be called on the after every write.

System_CAPS_pubproperty System_CAPS_static

Gets the correlation manager for the thread for this trace.

System_CAPS_pubproperty System_CAPS_static

Gets or sets the indent level.

System_CAPS_pubproperty System_CAPS_static

Gets or sets the number of spaces in an indent.

System_CAPS_pubproperty System_CAPS_static

Gets the collection of listeners that is monitoring the trace output.

System_CAPS_pubproperty System_CAPS_static

Gets or sets a value indicating whether the global lock should be used.

Methods
NameDescription
System_CAPS_pubmethod System_CAPS_static Assert

Checks for a condition; if the condition is false, displays a message box that shows the call stack.

System_CAPS_pubmethod System_CAPS_static Assert

Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.

System_CAPS_pubmethod System_CAPS_static Assert

Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

System_CAPS_pubmethod System_CAPS_static Close

Flushes the output buffer, and then closes the .

System_CAPS_pubmethod Equals

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethod System_CAPS_static Fail

Emits the specified error message.

System_CAPS_pubmethod System_CAPS_static Fail

Emits an error message, and a detailed error message.

System_CAPS_pubmethod System_CAPS_static Flush

Flushes the output buffer, and causes buffered data to be written to the .

System_CAPS_pubmethod GetHashCode

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod System_CAPS_static Indent

Increases the current by one.

System_CAPS_pubmethod System_CAPS_static Refresh

Refreshes the trace configuration data.

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethod System_CAPS_static TraceError

Writes an error message to the trace listeners in the collection using the specified message.

System_CAPS_pubmethod System_CAPS_static TraceError

Writes an error message to the trace listeners in the collection using the specified array of objects and formatting information.

System_CAPS_pubmethod System_CAPS_static TraceInformation

Writes an informational message to the trace listeners in the collection using the specified message.

System_CAPS_pubmethod System_CAPS_static TraceInformation

Writes an informational message to the trace listeners in the collection using the specified array of objects and formatting information.

System_CAPS_pubmethod System_CAPS_static TraceWarning

Writes a warning message to the trace listeners in the collection using the specified message.

System_CAPS_pubmethod System_CAPS_static TraceWarning

Writes a warning message to the trace listeners in the collection using the specified array of objects and formatting information.

System_CAPS_pubmethod System_CAPS_static Unindent

Decreases the current by one.

System_CAPS_pubmethod System_CAPS_static Write

Writes the value of the object's ToString method to the trace listeners in the collection.

System_CAPS_pubmethod System_CAPS_static Write

Writes a category name and the value of the object's ToString method to the trace listeners in the collection.

System_CAPS_pubmethod System_CAPS_static Write

Writes a message to the trace listeners in the collection.

System_CAPS_pubmethod System_CAPS_static Write

Writes a category name and a message to the trace listeners in the collection.

System_CAPS_pubmethod System_CAPS_static WriteIf

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

System_CAPS_pubmethod System_CAPS_static WriteIf

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

System_CAPS_pubmethod System_CAPS_static WriteIf

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

System_CAPS_pubmethod System_CAPS_static WriteIf

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

System_CAPS_pubmethod System_CAPS_static WriteLine

Writes the value of the object's ToString method to the trace listeners in the collection.

System_CAPS_pubmethod System_CAPS_static WriteLine

Writes a category name and the value of the object's ToString method to the trace listeners in the collection.

System_CAPS_pubmethod System_CAPS_static WriteLine

Writes a message to the trace listeners in the collection.

System_CAPS_pubmethod System_CAPS_static WriteLine

Writes a category name and message to the trace listeners in the collection.

System_CAPS_pubmethod System_CAPS_static WriteLineIf

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

System_CAPS_pubmethod System_CAPS_static WriteLineIf

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

System_CAPS_pubmethod System_CAPS_static WriteLineIf

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

System_CAPS_pubmethod System_CAPS_static WriteLineIf

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

Remarks

You can use the properties and methods in the Trace class to instrument release builds. Instrumentation allows you to monitor the health of your application running in real-life settings. Tracing helps you isolate problems and fix them without disturbing a running system.

This class provides methods to display an Assert dialog box, and to emit an assertion that will always Fail. This class provides write methods in the following variations: Write, WriteLine, WriteIf, and WriteLineIf.

The BooleanSwitch and TraceSwitch classes provide means to dynamically control the tracing output. You can modify the values of these switches without recompiling your application. For information on using the configuration file to set a switch, see the Switch class and the How to: Create, Initialize and Configure Trace Switches topic.

You can customize the tracing output's target by adding TraceListener instances to or removing instances from the collection. The collection is shared by both the Debug and the Trace classes; adding a trace listener to either class adds the listener to both. By default, trace output is emitted using the DefaultTraceListener class.

System_CAPS_noteNote

Adding a trace listener to the collection can cause an exception to be thrown while tracing, if a resource used by the trace listener is not available. The conditions and the exception thrown depend on the trace listener and cannot be enumerated in this topic. It may be useful to place calls to the Trace methods in try/catch blocks to detect and handle any exceptions from trace listeners.

System_CAPS_noteNote

If you add trace listeners to partially trusted code, you will get a SecurityException exception, because adding trace listeners requires UnmanagedCode permission. To trace partially trusted code that is running in a sandbox in Visual Studio, do not add trace listeners. Instead, view the Trace and Debug messages in the Output window.

The Trace class provides properties to get or set the level of Indent, the , and whether to after each write.

To set the and for Trace, you can edit the configuration file that corresponds to the name of your application. The configuration file should be formatted like the following example:

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="3" />
  </system.diagnostics>
</configuration>

The ConditionalAttribute attribute is applied to the methods of Trace. Compilers that support ConditionalAttribute ignore calls to these methods unless "TRACE" is defined as a conditional compilation symbol. Refer to a compiler's documentation to determine whether ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol.

System_CAPS_noteNote

In Visual Studio projects, by default, the "DEBUG" conditional compilation symbol is defined for debug builds, and the "TRACE" symbol is defined for both debug and release builds. For information about how to disable this behavior, see the Visual Studio documentation.

To define the "TRACE" conditional compilation symbol in C#, add the /d:TRACE option to the compiler command line when you compile your code using a command line, or add #define TRACE to the top of your file. In Visual Basic, add the /d:TRACE=True option to the compiler command line or add #Const TRACE=True to the file.

ConditionalAttribute is not supported by the C++ compiler. To provide equivalent functionality, you must enclose calls to the methods of Trace in an #if defined(TRACE) ... #endif block, and add the /DTRACE option to the compiler command line or add #define TRACE to the file.

Examples

The following example uses Trace to indicate the beginning and the end of a program's execution. The example also uses the TraceIndent and TraceUnindent methods to distinguish the tracing output. For a more complete example of the use of Trace, see How to: Add Trace Statements to Application Code.

// Specify /d:TRACE when compiling.

using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
       Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
       Trace.AutoFlush = true;
       Trace.Indent();
       Trace.WriteLine("Entering Main");
       Console.WriteLine("Hello World.");
       Trace.WriteLine("Exiting Main"); 
       Trace.Unindent();
    }
}
' Specify /d:TRACE=True when compiling.

Imports System
Imports System.Diagnostics

Class Test

    Shared Sub Main()

        Trace.Listeners.Add(New TextWriterTraceListener(Console.Out))
        Trace.AutoFlush = True
        Trace.Indent()
        Trace.WriteLine("Entering Main")
        Console.WriteLine("Hello World.")
        Trace.WriteLine("Exiting Main")
        Trace.Unindent()

    End Sub 'Main

End Class
// Specify /DTRACE when compiling.

#using <System.dll>
using namespace System;
using namespace System::Diagnostics;

int main()
{
   #if defined(TRACE)
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();
   Trace::WriteLine( "Entering Main" );
   #endif
   Console::WriteLine( "Hello World." );
   #if defined(TRACE)
   Trace::WriteLine( "Exiting Main" );
   Trace::Unindent();
   #endif
   return 0;
}
Security
SecurityPermission

for operating with unmanaged code. Security action: Demand. Associated enumeration: SecurityPermissionFlagUnmanagedCode

Version Information
Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Thread Safety

This type is thread safe.

Return to top