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)]
35 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
37 return TraceInternal.Listeners;
43 /// Gets or sets whether <see cref='System.Diagnostics.Trace.Flush'/> should be called on the <see cref='System.Diagnostics.Trace.Listeners'/> after every write.
46 public static bool AutoFlush {
47 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
49 return TraceInternal.AutoFlush;
52 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
54 TraceInternal.AutoFlush = value;
58 public static bool UseGlobalLock {
59 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
61 return TraceInternal.UseGlobalLock;
64 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
66 TraceInternal.UseGlobalLock = value;
70 public static CorrelationManager CorrelationManager {
71 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
73 if (correlationManager == null)
74 correlationManager = new CorrelationManager();
76 return correlationManager;
81 /// <para>Gets or sets the indent level.</para>
83 public static int IndentLevel {
84 get { return TraceInternal.IndentLevel; }
86 set { TraceInternal.IndentLevel = value; }
92 /// Gets or sets the number of spaces in an indent.
95 public static int IndentSize {
96 get { return TraceInternal.IndentSize; }
98 set { TraceInternal.IndentSize = value; }
102 /// <para>Clears the output buffer, and causes buffered data to
103 /// be written to the <see cref='System.Diagnostics.Trace.Listeners'/>.</para>
105 [System.Diagnostics.Conditional("TRACE")]
106 public static void Flush() {
107 TraceInternal.Flush();
111 /// <para>Clears the output buffer, and then closes the <see cref='System.Diagnostics.Trace.Listeners'/> so that they no
112 /// longer receive debugging output.</para>
114 [System.Diagnostics.Conditional("TRACE")]
115 public static void Close() {
118 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
120 TraceInternal.Close();
124 /// <para>Checks for a condition, and outputs the callstack if the
126 /// is <see langword='false'/>.</para>
128 [System.Diagnostics.Conditional("TRACE")]
129 public static void Assert(bool condition) {
130 TraceInternal.Assert(condition);
134 /// <para>Checks for a condition, and displays a message if the condition is
135 /// <see langword='false'/>. </para>
137 [System.Diagnostics.Conditional("TRACE")]
138 public static void Assert(bool condition, string message) {
139 TraceInternal.Assert(condition, message);
143 /// <para>Checks for a condition, and displays both messages if the condition
144 /// is <see langword='false'/>. </para>
146 [System.Diagnostics.Conditional("TRACE")]
147 public static void Assert(bool condition, string message, string detailMessage) {
148 TraceInternal.Assert(condition, message, detailMessage);
152 /// <para>Emits or displays a message for an assertion that always fails.</para>
154 [System.Diagnostics.Conditional("TRACE")]
155 public static void Fail(string message) {
156 TraceInternal.Fail(message);
160 /// <para>Emits or displays both messages for an assertion that always fails.</para>
162 [System.Diagnostics.Conditional("TRACE")]
163 public static void Fail(string message, string detailMessage) {
164 TraceInternal.Fail(message, detailMessage);
167 public static void Refresh() {
168 #if CONFIGURATION_DEP
169 DiagnosticsConfiguration.Refresh();
172 TraceSource.RefreshAll();
173 TraceInternal.Refresh();
176 [System.Diagnostics.Conditional("TRACE")]
177 public static void TraceInformation(string message) {
178 TraceInternal.TraceEvent(TraceEventType.Information, 0, message, null);
181 [System.Diagnostics.Conditional("TRACE")]
182 public static void TraceInformation(string format, params object[] args) {
183 TraceInternal.TraceEvent(TraceEventType.Information, 0, format, args);
186 [System.Diagnostics.Conditional("TRACE")]
187 public static void TraceWarning(string message) {
188 TraceInternal.TraceEvent(TraceEventType.Warning, 0, message, null);
191 [System.Diagnostics.Conditional("TRACE")]
192 public static void TraceWarning(string format, params object[] args) {
193 TraceInternal.TraceEvent(TraceEventType.Warning, 0, format, args);
196 [System.Diagnostics.Conditional("TRACE")]
197 public static void TraceError(string message) {
198 TraceInternal.TraceEvent(TraceEventType.Error, 0, message, null);
201 [System.Diagnostics.Conditional("TRACE")]
202 public static void TraceError(string format, params object[] args) {
203 TraceInternal.TraceEvent(TraceEventType.Error, 0, format, args);
207 /// <para>Writes a message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
208 /// collection.</para>
210 [System.Diagnostics.Conditional("TRACE")]
211 public static void Write(string message) {
212 TraceInternal.Write(message);
216 /// <para>Writes the name of the <paramref name="value "/>
217 /// parameter to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
219 [System.Diagnostics.Conditional("TRACE")]
220 public static void Write(object value) {
221 TraceInternal.Write(value);
225 /// <para>Writes a category name and message to the trace listeners
226 /// in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
228 [System.Diagnostics.Conditional("TRACE")]
229 public static void Write(string message, string category) {
230 TraceInternal.Write(message, category);
234 /// <para>Writes a category name and the name of the value parameter to the trace listeners
235 /// in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
237 [System.Diagnostics.Conditional("TRACE")]
238 public static void Write(object value, string category) {
239 TraceInternal.Write(value, category);
243 /// <para>Writes a message followed by a line terminator to the
244 /// trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.
245 /// The default line terminator is a carriage return followed by a line feed (\r\n).</para>
247 [System.Diagnostics.Conditional("TRACE")]
248 public static void WriteLine(string message) {
249 TraceInternal.WriteLine(message);
253 /// <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
254 /// terminator is a carriage return followed by a line feed (\r\n).</para>
256 [System.Diagnostics.Conditional("TRACE")]
257 public static void WriteLine(object value) {
258 TraceInternal.WriteLine(value);
262 /// <para>Writes a category name and message followed by a line terminator to the trace
263 /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
264 /// collection. The default line terminator is a carriage return followed by a line
265 /// feed (\r\n).</para>
267 [System.Diagnostics.Conditional("TRACE")]
268 public static void WriteLine(string message, string category) {
269 TraceInternal.WriteLine(message, category);
273 /// <para>Writes a <paramref name="category "/>name and the name of the <paramref name="value "/> parameter followed by a line
274 /// terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection. The default line
275 /// terminator is a carriage return followed by a line feed (\r\n).</para>
277 [System.Diagnostics.Conditional("TRACE")]
278 public static void WriteLine(object value, string category) {
279 TraceInternal.WriteLine(value, category);
283 /// <para>Writes a message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
284 /// if a condition is <see langword='true'/>.</para>
286 [System.Diagnostics.Conditional("TRACE")]
287 public static void WriteIf(bool condition, string message) {
288 TraceInternal.WriteIf(condition, message);
292 /// <para>Writes the name of the <paramref name="value "/>
293 /// parameter to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
294 /// <see langword='true'/>. </para>
296 [System.Diagnostics.Conditional("TRACE")]
297 public static void WriteIf(bool condition, object value) {
298 TraceInternal.WriteIf(condition, value);
302 /// <para>Writes a category name and message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
303 /// collection if a condition is <see langword='true'/>. </para>
305 [System.Diagnostics.Conditional("TRACE")]
306 public static void WriteIf(bool condition, string message, string category) {
307 TraceInternal.WriteIf(condition, message, category);
311 /// <para>Writes a category name and the name of the <paramref name="value"/> parameter to the trace
312 /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
313 /// if a condition is <see langword='true'/>. </para>
315 [System.Diagnostics.Conditional("TRACE")]
316 public static void WriteIf(bool condition, object value, string category) {
317 TraceInternal.WriteIf(condition, value, category);
321 /// <para>Writes a message followed by a line terminator to the trace listeners in the
322 /// <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
323 /// <see langword='true'/>. The default line terminator is a carriage return followed
324 /// by a line feed (\r\n).</para>
326 [System.Diagnostics.Conditional("TRACE")]
327 public static void WriteLineIf(bool condition, string message) {
328 TraceInternal.WriteLineIf(condition, message);
332 /// <para>Writes the name of the <paramref name="value"/> parameter followed by a line terminator to the
333 /// trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
334 /// if a condition is
335 /// <see langword='true'/>. The default line
336 /// terminator is a carriage return followed by a line feed (\r\n).</para>
338 [System.Diagnostics.Conditional("TRACE")]
339 public static void WriteLineIf(bool condition, object value) {
340 TraceInternal.WriteLineIf(condition, value);
344 /// <para>Writes a category name and message followed by a line terminator to the trace
345 /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
346 /// <see langword='true'/>. The default line terminator is a carriage return followed by a line feed (\r\n).</para>
348 [System.Diagnostics.Conditional("TRACE")]
349 public static void WriteLineIf(bool condition, string message, string category) {
350 TraceInternal.WriteLineIf(condition, message, category);
354 /// <para>Writes a category name and the name of the <paramref name="value "/> parameter followed by a line
355 /// terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
356 /// if a <paramref name="condition"/> is <see langword='true'/>. The
357 /// default line terminator is a carriage return followed by a line feed (\r\n).</para>
359 [System.Diagnostics.Conditional("TRACE")]
360 public static void WriteLineIf(bool condition, object value, string category) {
361 TraceInternal.WriteLineIf(condition, value, category);
365 /// <para>[To be supplied.]</para>
367 [System.Diagnostics.Conditional("TRACE")]
368 public static void Indent() {
369 TraceInternal.Indent();
373 /// <para>[To be supplied.]</para>
375 [System.Diagnostics.Conditional("TRACE")]
376 public static void Unindent() {
377 TraceInternal.Unindent();