1 out of 1 rated this helpful Rate this topic

Trace Class

Updated: June 2010

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

System.Object
  System.Diagnostics.Trace

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)
public sealed class Trace

The Trace type exposes the following members.

  Name Description
Public property Static member Supported by the XNA Framework AutoFlush Gets or sets whether Flush should be called on the Listeners after every write.
Public property Static member CorrelationManager Gets the correlation manager for the thread for this trace.
Public property Static member Supported by the XNA Framework IndentLevel Gets or sets the indent level.
Public property Static member Supported by the XNA Framework IndentSize Gets or sets the number of spaces in an indent.
Public property Static member Supported by the XNA Framework Listeners Gets the collection of listeners that is monitoring the trace output.
Public property Static member UseGlobalLock Gets or sets a value indicating whether the global lock should be used.
Top
  Name Description
Public method Static member Supported by the XNA Framework Assert(Boolean) Checks for a condition; if the condition is false, displays a message box that shows the call stack.
Public method Static member Supported by the XNA Framework Assert(Boolean, String) Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.
Public method Static member Supported by the XNA Framework Assert(Boolean, String, String) Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.
Public method Static member Supported by the XNA Framework Close Flushes the output buffer, and then closes the Listeners.
Public method Supported by the XNA Framework Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Fail(String) Emits the specified error message.
Public method Static member Supported by the XNA Framework Fail(String, String) Emits an error message, and a detailed error message.
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Flush Flushes the output buffer, and causes buffered data to be written to the Listeners.
Public method Supported by the XNA Framework GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Static member Indent Increases the current IndentLevel by one.
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member Refresh Refreshes the trace configuration data.
Public method Supported by the XNA Framework ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Static member TraceError(String) Writes an error message to the trace listeners in the Listeners collection using the specified message.
Public method Static member TraceError(String, Object()) Writes an error message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.
Public method Static member TraceInformation(String) Writes an informational message to the trace listeners in the Listeners collection using the specified message.
Public method Static member TraceInformation(String, Object()) Writes an informational message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.
Public method Static member TraceWarning(String) Writes a warning message to the trace listeners in the Listeners collection using the specified message.
Public method Static member TraceWarning(String, Object()) Writes a warning message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.
Public method Static member Unindent Decreases the current IndentLevel by one.
Public method Static member Supported by the XNA Framework Write(Object) Writes the value of the object's ToString method to the trace listeners in the Listeners collection.
Public method Static member Supported by the XNA Framework Write(String) Writes a message to the trace listeners in the Listeners collection.
Public method Static member Supported by the XNA Framework Write(Object, String) Writes a category name and the value of the object's ToString method to the trace listeners in the Listeners collection.
Public method Static member Supported by the XNA Framework Write(String, String) Writes a category name and a message to the trace listeners in the Listeners collection.
Public method Static member Supported by the XNA Framework WriteIf(Boolean, Object) Writes the value of the object's ToString method to the trace listeners in the Listeners collection if a condition is true.
Public method Static member Supported by the XNA Framework WriteIf(Boolean, String) Writes a message to the trace listeners in the Listeners collection if a condition is true.
Public method Static member Supported by the XNA Framework WriteIf(Boolean, Object, 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.
Public method Static member Supported by the XNA Framework WriteIf(Boolean, String, String) Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.
Public method Static member Supported by the XNA Framework WriteLine(Object) Writes the value of the object's ToString method to the trace listeners in the Listeners collection.
Public method Static member Supported by the XNA Framework WriteLine(String) Writes a message to the trace listeners in the Listeners collection.
Public method Static member Supported by the XNA Framework WriteLine(Object, String) Writes a category name and the value of the object's ToString method to the trace listeners in the Listeners collection.
Public method Static member Supported by the XNA Framework WriteLine(String, String) Writes a category name and message to the trace listeners in the Listeners collection.
Public method Static member Supported by the XNA Framework WriteLineIf(Boolean, Object) Writes the value of the object's ToString method to the trace listeners in the Listeners collection if a condition is true.
Public method Static member Supported by the XNA Framework WriteLineIf(Boolean, String) Writes a message to the trace listeners in the Listeners collection if a condition is true.
Public method Static member Supported by the XNA Framework WriteLineIf(Boolean, Object, 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.
Public method Static member Supported by the XNA Framework WriteLineIf(Boolean, String, String) Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.
Top

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: Configure Trace Switches topic.

You can customize the tracing output's target by adding TraceListener instances to or removing instances from the Listeners collection. The Listeners 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.

Note Note

Adding a trace listener to the Listeners 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.

Note Note

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 IndentSize, and whether to AutoFlush after each write.

To set the AutoFlush and IndentSize 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.

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.

The following example uses Trace to indicate the beginning and the end of a program's execution. The example also uses the Trace.Indent and Trace.Unindent 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();
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
  • SecurityPermission  

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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

This type is thread safe.

Date

History

Reason

June 2010

Added a link to a complete example from the Examples section.

Customer feedback.

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
Diagnostic Traces

To enable and output tracing for the System.Transactions.

See the following link:

http://msdn.microsoft.com/en-us/library/ms229689(v=VS.90).aspx

antonio

Wrong hyperlinks in Remarks section
Unfortunately, all of the links to further topics in the table of the Remarks section refer to the wrong pages. As you can see, the URLs contain a "(v=VS.80)" or "(v=VS.85)" part. They lead you to the pages of former .NET Framework versions, but not to the ones for the current version 4.0, although they are existing.
Error Id

Why isn't Information Id incremented after TraceError("Some Info") calls
 So for example I have a foolowing code snippet:
ConsoleTraceListener tl = new ConsoleTraceListener();
Trace.Listeners.Add(tl);
Trace.TraceError("some error");
Trace.TraceError("some other error");
Trace.Flush();
Trace.Close();

The output is:
System.Diagnostics.Test.vshost.exe Error: 0 : some error
System.Diagnostics.Test.vshost.exe Error: 0 : some other error

Should error Id be incremented after each call of the TraceError method?