This class supplies static
methods and properties to
provide tracing ability. The calls to the Trace
methods and properties are executed only if tracing is enabled. (See
the introduction to this chapter for instructions on enabling
tracing.)
The static properties allow you to adjust the settings that are used
when you call the methods. You can specify that output be indented a
certain amount with IndentLevel or increase or
decrease the IndentLevel by one using
Indent( ) and Unindent( ). You
can also adjust the number of spaces each indent level adds using
IndentSize. AutoFlush makes
sure that after each use of a Trace method, the
Listeners are flushed.
Write( ) and WriteLine( )
simply write to each TraceListener in the
Listeners collection (by default, this collection
includes an instance of DefaultTraceListener).
WriteIf( ) and WriteLineIf( )
do the same, but only if the specified condition evaluates to
true. Assert( ) emits an error
message if a condition evaluates to false, and
Fail( ) always emits an error message.
One possible point of confusion is that Listeners
is read-only. This means that you may not point
Listeners to a different collection. You can,
however, add new TraceListener objects to the
TraceListenerCollection with the
TraceListenerCollection.Add( ) method.
You can use the application configuration file to configure this
class. Under the <system.diagnostics>
element, add a <trace> element. You can set
attributes for this element that correspond to
Trace properties, as in <trace
autoflush="true" indentsize="4"/>.
public sealed class Trace {
// Public Static Properties
public static bool AutoFlush{set; get; }
public static int IndentLevel{set; get; }
public static int IndentSize{set; get; }
public static TraceListenerCollection Listeners{get; }
// Public Static Methods
public static void Assert(bool condition);
public static void Assert(bool condition, string message);
public static void Assert(bool condition, string message, string detailMessage);
public static void Close( );
public static void Fail(string message);
public static void Fail(string message, string detailMessage);
public static void Flush( );
public static void Indent( );
public static void Unindent( );
public static void Write(object value);
public static void Write(object value, string category);
public static void Write(string message);
public static void Write(string message, string category);
public static void WriteIf(bool condition, object value);
public static void WriteIf(bool condition, object value, string category);
public static void WriteIf(bool condition, string message);
public static void WriteIf(bool condition, string message, string category);
public static void WriteLine(object value);
public static void WriteLine(object value, string category);
public static void WriteLine(string message);
public static void WriteLine(string message, string category);
public static void WriteLineIf(bool condition, object value);
public static void WriteLineIf(bool condition, object value, string category);
public static void WriteLineIf(bool condition, string message);
public static void WriteLineIf(bool condition, string message, string category);
}