1 //------------------------------------------------------------------------------
2 // <copyright file="Trace.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
10 namespace System.Diagnostics {
12 using System.Collections;
13 using System.Security.Permissions;
14 using System.Threading;
17 /// <para>Provides a set of properties and methods to trace the execution of your code.</para>
19 public sealed class Trace {
20 private static volatile CorrelationManager correlationManager = null;
28 /// <para>Gets the collection of listeners that is monitoring the trace output.</para>
30 public static TraceListenerCollection Listeners {
31 [HostProtection(SharedState=true)]
34 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
36 return TraceInternal.Listeners;
42 /// Gets or sets whether <see cref='System.Diagnostics.Trace.Flush'/> should be called on the <see cref='System.Diagnostics.Trace.Listeners'/> after every write.
45 public static bool AutoFlush {
46 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
48 return TraceInternal.AutoFlush;
51 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
53 TraceInternal.AutoFlush = value;
57 public static bool UseGlobalLock {
58 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
60 return TraceInternal.UseGlobalLock;
63 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
65 TraceInternal.UseGlobalLock = value;
69 public static CorrelationManager CorrelationManager {
70 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
72 if (correlationManager == null)
73 correlationManager = new CorrelationManager();
75 return correlationManager;
80 /// <para>Gets or sets the indent level.</para>
82 public static int IndentLevel {
83 get { return TraceInternal.IndentLevel; }
85 set { TraceInternal.IndentLevel = value; }
91 /// Gets or sets the number of spaces in an indent.
94 public static int IndentSize {
95 get { return TraceInternal.IndentSize; }
97 set { TraceInternal.IndentSize = value; }
101 /// <para>Clears the output buffer, and causes buffered data to
102 /// be written to the <see cref='System.Diagnostics.Trace.Listeners'/>.</para>
104 [System.Diagnostics.Conditional("TRACE")]
105 public static void Flush() {
106 TraceInternal.Flush();
110 /// <para>Clears the output buffer, and then closes the <see cref='System.Diagnostics.Trace.Listeners'/> so that they no
111 /// longer receive debugging output.</para>
113 [System.Diagnostics.Conditional("TRACE")]
114 public static void Close() {
116 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
118 TraceInternal.Close();
122 /// <para>Checks for a condition, and outputs the callstack if the
124 /// is <see langword='false'/>.</para>
126 [System.Diagnostics.Conditional("TRACE")]
127 public static void Assert(bool condition) {
128 TraceInternal.Assert(condition);
132 /// <para>Checks for a condition, and displays a message if the condition is
133 /// <see langword='false'/>. </para>
135 [System.Diagnostics.Conditional("TRACE")]
136 public static void Assert(bool condition, string message) {
137 TraceInternal.Assert(condition, message);
141 /// <para>Checks for a condition, and displays both messages if the condition
142 /// is <see langword='false'/>. </para>
144 [System.Diagnostics.Conditional("TRACE")]
145 public static void Assert(bool condition, string message, string detailMessage) {
146 TraceInternal.Assert(condition, message, detailMessage);
150 /// <para>Emits or displays a message for an assertion that always fails.</para>
152 [System.Diagnostics.Conditional("TRACE")]
153 public static void Fail(string message) {
154 TraceInternal.Fail(message);
158 /// <para>Emits or displays both messages for an assertion that always fails.</para>
160 [System.Diagnostics.Conditional("TRACE")]
161 public static void Fail(string message, string detailMessage) {
162 TraceInternal.Fail(message, detailMessage);
165 public static void Refresh() {
166 #if CONFIGURATION_DEP
167 DiagnosticsConfiguration.Refresh();
170 TraceSource.RefreshAll();
171 TraceInternal.Refresh();
174 [System.Diagnostics.Conditional("TRACE")]
175 public static void TraceInformation(string message) {
176 TraceInternal.TraceEvent(TraceEventType.Information, 0, message, null);
179 [System.Diagnostics.Conditional("TRACE")]
180 public static void TraceInformation(string format, params object[] args) {
181 TraceInternal.TraceEvent(TraceEventType.Information, 0, format, args);
184 [System.Diagnostics.Conditional("TRACE")]
185 public static void TraceWarning(string message) {
186 TraceInternal.TraceEvent(TraceEventType.Warning, 0, message, null);
189 [System.Diagnostics.Conditional("TRACE")]
190 public static void TraceWarning(string format, params object[] args) {
191 TraceInternal.TraceEvent(TraceEventType.Warning, 0, format, args);
194 [System.Diagnostics.Conditional("TRACE")]
195 public static void TraceError(string message) {
196 TraceInternal.TraceEvent(TraceEventType.Error, 0, message, null);
199 [System.Diagnostics.Conditional("TRACE")]
200 public static void TraceError(string format, params object[] args) {
201 TraceInternal.TraceEvent(TraceEventType.Error, 0, format, args);
205 /// <para>Writes a message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
206 /// collection.</para>
208 [System.Diagnostics.Conditional("TRACE")]
209 public static void Write(string message) {
210 TraceInternal.Write(message);
214 /// <para>Writes the name of the <paramref name="value "/>
215 /// parameter to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
217 [System.Diagnostics.Conditional("TRACE")]
218 public static void Write(object value) {
219 TraceInternal.Write(value);
223 /// <para>Writes a category name and message to the trace listeners
224 /// in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
226 [System.Diagnostics.Conditional("TRACE")]
227 public static void Write(string message, string category) {
228 TraceInternal.Write(message, category);
232 /// <para>Writes a category name and the name of the value parameter to the trace listeners
233 /// in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
235 [System.Diagnostics.Conditional("TRACE")]
236 public static void Write(object value, string category) {
237 TraceInternal.Write(value, category);
241 /// <para>Writes a message followed by a line terminator to the
242 /// trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.
243 /// The default line terminator is a carriage return followed by a line feed (\r\n).</para>
245 [System.Diagnostics.Conditional("TRACE")]
246 public static void WriteLine(string message) {
247 TraceInternal.WriteLine(message);
251 /// <para>Writes the name of the <paramref name="value "/> parameter followed by a line terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection. The default line
252 /// terminator is a carriage return followed by a line feed (\r\n).</para>
254 [System.Diagnostics.Conditional("TRACE")]
255 public static void WriteLine(object value) {
256 TraceInternal.WriteLine(value);
260 /// <para>Writes a category name and message followed by a line terminator to the trace
261 /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
262 /// collection. The default line terminator is a carriage return followed by a line
263 /// feed (\r\n).</para>
265 [System.Diagnostics.Conditional("TRACE")]
266 public static void WriteLine(string message, string category) {
267 TraceInternal.WriteLine(message, category);
271 /// <para>Writes a <paramref name="category "/>name and the name of the <paramref name="value "/> parameter followed by a line
272 /// terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection. The default line
273 /// terminator is a carriage return followed by a line feed (\r\n).</para>
275 [System.Diagnostics.Conditional("TRACE")]
276 public static void WriteLine(object value, string category) {
277 TraceInternal.WriteLine(value, category);
281 /// <para>Writes a message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
282 /// if a condition is <see langword='true'/>.</para>
284 [System.Diagnostics.Conditional("TRACE")]
285 public static void WriteIf(bool condition, string message) {
286 TraceInternal.WriteIf(condition, message);
290 /// <para>Writes the name of the <paramref name="value "/>
291 /// parameter to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
292 /// <see langword='true'/>. </para>
294 [System.Diagnostics.Conditional("TRACE")]
295 public static void WriteIf(bool condition, object value) {
296 TraceInternal.WriteIf(condition, value);
300 /// <para>Writes a category name and message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
301 /// collection if a condition is <see langword='true'/>. </para>
303 [System.Diagnostics.Conditional("TRACE")]
304 public static void WriteIf(bool condition, string message, string category) {
305 TraceInternal.WriteIf(condition, message, category);
309 /// <para>Writes a category name and the name of the <paramref name="value"/> parameter to the trace
310 /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
311 /// if a condition is <see langword='true'/>. </para>
313 [System.Diagnostics.Conditional("TRACE")]
314 public static void WriteIf(bool condition, object value, string category) {
315 TraceInternal.WriteIf(condition, value, category);
319 /// <para>Writes a message followed by a line terminator to the trace listeners in the
320 /// <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
321 /// <see langword='true'/>. The default line terminator is a carriage return followed
322 /// by a line feed (\r\n).</para>
324 [System.Diagnostics.Conditional("TRACE")]
325 public static void WriteLineIf(bool condition, string message) {
326 TraceInternal.WriteLineIf(condition, message);
330 /// <para>Writes the name of the <paramref name="value"/> parameter followed by a line terminator to the
331 /// trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
332 /// if a condition is
333 /// <see langword='true'/>. The default line
334 /// terminator is a carriage return followed by a line feed (\r\n).</para>
336 [System.Diagnostics.Conditional("TRACE")]
337 public static void WriteLineIf(bool condition, object value) {
338 TraceInternal.WriteLineIf(condition, value);
342 /// <para>Writes a category name and message followed by a line terminator to the trace
343 /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
344 /// <see langword='true'/>. The default line terminator is a carriage return followed by a line feed (\r\n).</para>
346 [System.Diagnostics.Conditional("TRACE")]
347 public static void WriteLineIf(bool condition, string message, string category) {
348 TraceInternal.WriteLineIf(condition, message, category);
352 /// <para>Writes a category name and the name of the <paramref name="value "/> parameter followed by a line
353 /// terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
354 /// if a <paramref name="condition"/> is <see langword='true'/>. The
355 /// default line terminator is a carriage return followed by a line feed (\r\n).</para>
357 [System.Diagnostics.Conditional("TRACE")]
358 public static void WriteLineIf(bool condition, object value, string category) {
359 TraceInternal.WriteLineIf(condition, value, category);
363 /// <para>[To be supplied.]</para>
365 [System.Diagnostics.Conditional("TRACE")]
366 public static void Indent() {
367 TraceInternal.Indent();
371 /// <para>[To be supplied.]</para>
373 [System.Diagnostics.Conditional("TRACE")]
374 public static void Unindent() {
375 TraceInternal.Unindent();