Provides a set of properties and methods to trace the execution of your code.
Object
Trace
[Visual Basic] Public Class Trace [C#] public class Trace [C++] public __gc class Trace [JScript] public class Trace
You can use the properties and methods in the Trace class to instrument retail builds. By default, Trace is on in retail builds. Therefore, code is generated for all methods marked with Conditional("TRACE"). This allows a user to turn on tracing to help identify the problem without you recompiling the program. In contrast, by default Debug is off in retail builds.
Note To enable tracing, you must add the "/D:TRACE" flag to the compiler command line when you compile your code.
The BooleanSwitch and TraceSwitch classes provide a means to control the tracing output dynamically. The values of these switches can be modified outside the compiled application.
You can customize the tracing output's target by adding TraceListener instances to or removing instances from the Listeners collection. By default, trace output is emitted using the DefaultTraceListener class.
The Trace class provides properties to get or set the level of Indent, the IndentSize and whether to AutoFlush after each write.
This class also provides methods to display an Assert dialog box, and to set an assertion that will always Fail. This class provides write methods in the following variations: Write, WriteLine, WriteIf and WriteLineIf.
For more information, see <DebugTopicTBD>.
Namespace: System.Diagnostics
Assembly: System.dll
The following example creates three switches to control the tracing output for the Customer class.
[C#]
Class Customer{ // trace each method flow static BooleanSwitch traceMethods("TraceMethods", "Trace logic flow"); // trace method parameters static BooleanSwitch traceParams("TraceParams", "Trace parameter values"); // trace return values static BooleanSwitch traceResults("TraceResults", "Trace calculations"); string Name(string last, string first){ Trace.WriteLineIf(traceMethods.Enabled, "Name called"); Trace.WriteLineIf(traceParams.Enabled, "Params: " + last + ", " + first); string fullName = first + " " + last; Trace.WriteLineIf(traceMethods.Enabled, "Name terminates"); Trace.WriteLineIf(traceResults.Enabled, "Result: " + fullName); return fullName; } }
If the traceMethods and traceResults are set to true, and traceParams is set to false, the output for a call to Name("Smith", "John") would be:
Name called
Name terminates
Result: John Smith
Trace Members | System.Diagnostics Namespace | Debug | BooleanSwitch | TraceSwitch | TraceListener | DefaultTraceListener | ConditionalAttribute