The Labs \ Source Viewer \ SSCLI \ System.Diagnostics \ Trace

  1. //------------------------------------------------------------------------------
  2. // <copyright file="Trace.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. #define TRACE
  18. namespace System.Diagnostics
  19. {
  20.     using System;
  21.     using System.Collections;
  22.     using System.Security.Permissions;
  23.     using System.Threading;
  24.    
  25.     /// <devdoc>
  26.     /// <para>Provides a set of properties and methods to trace the execution of your code.</para>
  27.     /// </devdoc>
  28.     public sealed class Trace
  29.     {
  30.         private static CorrelationManager correlationManager = null;
  31.        
  32.         // not creatble...
  33.         //
  34.         private Trace()
  35.         {
  36.         }
  37.        
  38.         /// <devdoc>
  39.         /// <para>Gets the collection of listeners that is monitoring the trace output.</para>
  40.         /// </devdoc>
  41.         public static TraceListenerCollection Listeners {
  42.             [HostProtection(SharedState = true)]
  43.             get {
  44.                 // Do a full damand
  45.                 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
  46.                
  47.                 return TraceInternal.Listeners;
  48.             }
  49.         }
  50.        
  51.         /// <devdoc>
  52.         /// <para>
  53.         /// Gets or sets whether <see cref='System.Diagnostics.Trace.Flush'/> should be called on the <see cref='System.Diagnostics.Trace.Listeners'/> after every write.
  54.         /// </para>
  55.         /// </devdoc>
  56.         public static bool AutoFlush {
  57.             [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  58.             get { return TraceInternal.AutoFlush; }
  59.            
  60.             [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  61.             set { TraceInternal.AutoFlush = value; }
  62.         }
  63.        
  64.         public static bool UseGlobalLock {
  65.             [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  66.             get { return TraceInternal.UseGlobalLock; }
  67.            
  68.             [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  69.             set { TraceInternal.UseGlobalLock = value; }
  70.         }
  71.        
  72.         public static CorrelationManager CorrelationManager {
  73.             [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  74.             get {
  75.                 if (correlationManager == null)
  76.                     correlationManager = new CorrelationManager();
  77.                
  78.                 return correlationManager;
  79.             }
  80.         }
  81.        
  82.         /// <devdoc>
  83.         /// <para>Gets or sets the indent level.</para>
  84.         /// </devdoc>
  85.         public static int IndentLevel {
  86.             get { return TraceInternal.IndentLevel; }
  87.            
  88.             set { TraceInternal.IndentLevel = value; }
  89.         }
  90.        
  91.        
  92.         /// <devdoc>
  93.         /// <para>
  94.         /// Gets or sets the number of spaces in an indent.
  95.         /// </para>
  96.         /// </devdoc>
  97.         public static int IndentSize {
  98.             get { return TraceInternal.IndentSize; }
  99.            
  100.             set { TraceInternal.IndentSize = value; }
  101.         }
  102.        
  103.         /// <devdoc>
  104.         /// <para>Clears the output buffer, and causes buffered data to
  105.         /// be written to the <see cref='System.Diagnostics.Trace.Listeners'/>.</para>
  106.         /// </devdoc>
  107.         [System.Diagnostics.Conditional("TRACE")]
  108.         public static void Flush()
  109.         {
  110.             TraceInternal.Flush();
  111.         }
  112.        
  113.         /// <devdoc>
  114.         /// <para>Clears the output buffer, and then closes the <see cref='System.Diagnostics.Trace.Listeners'/> so that they no
  115.         /// longer receive debugging output.</para>
  116.         /// </devdoc>
  117.         [System.Diagnostics.Conditional("TRACE")]
  118.         public static void Close()
  119.         {
  120.             // Do a full damand
  121.             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
  122.            
  123.             TraceInternal.Close();
  124.         }
  125.        
  126.         /// <devdoc>
  127.         /// <para>Checks for a condition, and outputs the callstack if the
  128.         /// condition
  129.         /// is <see langword='false'/>.</para>
  130.         /// </devdoc>
  131.         [System.Diagnostics.Conditional("TRACE")]
  132.         public static void Assert(bool condition)
  133.         {
  134.             TraceInternal.Assert(condition);
  135.         }
  136.        
  137.         /// <devdoc>
  138.         /// <para>Checks for a condition, and displays a message if the condition is
  139.         /// <see langword='false'/>. </para>
  140.         /// </devdoc>
  141.         [System.Diagnostics.Conditional("TRACE")]
  142.         public static void Assert(bool condition, string message)
  143.         {
  144.             TraceInternal.Assert(condition, message);
  145.         }
  146.        
  147.         /// <devdoc>
  148.         /// <para>Checks for a condition, and displays both messages if the condition
  149.         /// is <see langword='false'/>. </para>
  150.         /// </devdoc>
  151.         [System.Diagnostics.Conditional("TRACE")]
  152.         public static void Assert(bool condition, string message, string detailMessage)
  153.         {
  154.             TraceInternal.Assert(condition, message, detailMessage);
  155.         }
  156.        
  157.         /// <devdoc>
  158.         /// <para>Emits or displays a message for an assertion that always fails.</para>
  159.         /// </devdoc>
  160.         [System.Diagnostics.Conditional("TRACE")]
  161.         public static void Fail(string message)
  162.         {
  163.             TraceInternal.Fail(message);
  164.         }
  165.        
  166.         /// <devdoc>
  167.         /// <para>Emits or displays both messages for an assertion that always fails.</para>
  168.         /// </devdoc>
  169.         [System.Diagnostics.Conditional("TRACE")]
  170.         public static void Fail(string message, string detailMessage)
  171.         {
  172.             TraceInternal.Fail(message, detailMessage);
  173.         }
  174.        
  175.         public static void Refresh()
  176.         {
  177.             DiagnosticsConfiguration.Refresh();
  178.             Switch.RefreshAll();
  179.             TraceSource.RefreshAll();
  180.         }
  181.        
  182.         [System.Diagnostics.Conditional("TRACE")]
  183.         public static void TraceInformation(string message)
  184.         {
  185.             TraceInternal.TraceEvent(TraceEventType.Information, 0, message, null);
  186.         }
  187.        
  188.         [System.Diagnostics.Conditional("TRACE")]
  189.         public static void TraceInformation(string format, params object[] args)
  190.         {
  191.             TraceInternal.TraceEvent(TraceEventType.Information, 0, format, args);
  192.         }
  193.        
  194.         [System.Diagnostics.Conditional("TRACE")]
  195.         public static void TraceWarning(string message)
  196.         {
  197.             TraceInternal.TraceEvent(TraceEventType.Warning, 0, message, null);
  198.         }
  199.        
  200.         [System.Diagnostics.Conditional("TRACE")]
  201.         public static void TraceWarning(string format, params object[] args)
  202.         {
  203.             TraceInternal.TraceEvent(TraceEventType.Warning, 0, format, args);
  204.         }
  205.        
  206.         [System.Diagnostics.Conditional("TRACE")]
  207.         public static void TraceError(string message)
  208.         {
  209.             TraceInternal.TraceEvent(TraceEventType.Error, 0, message, null);
  210.         }
  211.        
  212.         [System.Diagnostics.Conditional("TRACE")]
  213.         public static void TraceError(string format, params object[] args)
  214.         {
  215.             TraceInternal.TraceEvent(TraceEventType.Error, 0, format, args);
  216.         }
  217.        
  218.         /// <devdoc>
  219.         /// <para>Writes a message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
  220.         /// collection.</para>
  221.         /// </devdoc>
  222.         [System.Diagnostics.Conditional("TRACE")]
  223.         public static void Write(string message)
  224.         {
  225.             TraceInternal.Write(message);
  226.         }
  227.        
  228.         /// <devdoc>
  229.         /// <para>Writes the name of the <paramref name="value "/>
  230.         /// parameter to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
  231.         /// </devdoc>
  232.         [System.Diagnostics.Conditional("TRACE")]
  233.         public static void Write(object value)
  234.         {
  235.             TraceInternal.Write(value);
  236.         }
  237.        
  238.         /// <devdoc>
  239.         /// <para>Writes a category name and message to the trace listeners
  240.         /// in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
  241.         /// </devdoc>
  242.         [System.Diagnostics.Conditional("TRACE")]
  243.         public static void Write(string message, string category)
  244.         {
  245.             TraceInternal.Write(message, category);
  246.         }
  247.        
  248.         /// <devdoc>
  249.         /// <para>Writes a category name and the name of the value parameter to the trace listeners
  250.         /// in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
  251.         /// </devdoc>
  252.         [System.Diagnostics.Conditional("TRACE")]
  253.         public static void Write(object value, string category)
  254.         {
  255.             TraceInternal.Write(value, category);
  256.         }
  257.        
  258.         /// <devdoc>
  259.         /// <para>Writes a message followed by a line terminator to the
  260.         /// trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.
  261.         /// The default line terminator is a carriage return followed by a line feed (\r\n).</para>
  262.         /// </devdoc>
  263.         [System.Diagnostics.Conditional("TRACE")]
  264.         public static void WriteLine(string message)
  265.         {
  266.             TraceInternal.WriteLine(message);
  267.         }
  268.        
  269.         /// <devdoc>
  270.         /// <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
  271.         /// terminator is a carriage return followed by a line feed (\r\n).</para>
  272.         /// </devdoc>
  273.         [System.Diagnostics.Conditional("TRACE")]
  274.         public static void WriteLine(object value)
  275.         {
  276.             TraceInternal.WriteLine(value);
  277.         }
  278.        
  279.         /// <devdoc>
  280.         /// <para>Writes a category name and message followed by a line terminator to the trace
  281.         /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
  282.         /// collection. The default line terminator is a carriage return followed by a line
  283.         /// feed (\r\n).</para>
  284.         /// </devdoc>
  285.         [System.Diagnostics.Conditional("TRACE")]
  286.         public static void WriteLine(string message, string category)
  287.         {
  288.             TraceInternal.WriteLine(message, category);
  289.         }
  290.        
  291.         /// <devdoc>
  292.         /// <para>Writes a <paramref name="category "/>name and the name of the <paramref name="value "/> parameter followed by a line
  293.         /// terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection. The default line
  294.         /// terminator is a carriage return followed by a line feed (\r\n).</para>
  295.         /// </devdoc>
  296.         [System.Diagnostics.Conditional("TRACE")]
  297.         public static void WriteLine(object value, string category)
  298.         {
  299.             TraceInternal.WriteLine(value, category);
  300.         }
  301.        
  302.         /// <devdoc>
  303.         /// <para>Writes a message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
  304.         /// if a condition is <see langword='true'/>.</para>
  305.         /// </devdoc>
  306.         [System.Diagnostics.Conditional("TRACE")]
  307.         public static void WriteIf(bool condition, string message)
  308.         {
  309.             TraceInternal.WriteIf(condition, message);
  310.         }
  311.        
  312.         /// <devdoc>
  313.         /// <para>Writes the name of the <paramref name="value "/>
  314.         /// parameter to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
  315.         /// <see langword='true'/>. </para>
  316.         /// </devdoc>
  317.         [System.Diagnostics.Conditional("TRACE")]
  318.         public static void WriteIf(bool condition, object value)
  319.         {
  320.             TraceInternal.WriteIf(condition, value);
  321.         }
  322.        
  323.         /// <devdoc>
  324.         /// <para>Writes a category name and message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
  325.         /// collection if a condition is <see langword='true'/>. </para>
  326.         /// </devdoc>
  327.         [System.Diagnostics.Conditional("TRACE")]
  328.         public static void WriteIf(bool condition, string message, string category)
  329.         {
  330.             TraceInternal.WriteIf(condition, message, category);
  331.         }
  332.        
  333.         /// <devdoc>
  334.         /// <para>Writes a category name and the name of the <paramref name="value"/> parameter to the trace
  335.         /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
  336.         /// if a condition is <see langword='true'/>. </para>
  337.         /// </devdoc>
  338.         [System.Diagnostics.Conditional("TRACE")]
  339.         public static void WriteIf(bool condition, object value, string category)
  340.         {
  341.             TraceInternal.WriteIf(condition, value, category);
  342.         }
  343.        
  344.         /// <devdoc>
  345.         /// <para>Writes a message followed by a line terminator to the trace listeners in the
  346.         /// <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
  347.         /// <see langword='true'/>. The default line terminator is a carriage return followed
  348.         /// by a line feed (\r\n).</para>
  349.         /// </devdoc>
  350.         [System.Diagnostics.Conditional("TRACE")]
  351.         public static void WriteLineIf(bool condition, string message)
  352.         {
  353.             TraceInternal.WriteLineIf(condition, message);
  354.         }
  355.        
  356.         /// <devdoc>
  357.         /// <para>Writes the name of the <paramref name="value"/> parameter followed by a line terminator to the
  358.         /// trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
  359.         /// if a condition is
  360.         /// <see langword='true'/>. The default line
  361.         /// terminator is a carriage return followed by a line feed (\r\n).</para>
  362.         /// </devdoc>
  363.         [System.Diagnostics.Conditional("TRACE")]
  364.         public static void WriteLineIf(bool condition, object value)
  365.         {
  366.             TraceInternal.WriteLineIf(condition, value);
  367.         }
  368.        
  369.         /// <devdoc>
  370.         /// <para>Writes a category name and message followed by a line terminator to the trace
  371.         /// listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
  372.         /// <see langword='true'/>. The default line terminator is a carriage return followed by a line feed (\r\n).</para>
  373.         /// </devdoc>
  374.         [System.Diagnostics.Conditional("TRACE")]
  375.         public static void WriteLineIf(bool condition, string message, string category)
  376.         {
  377.             TraceInternal.WriteLineIf(condition, message, category);
  378.         }
  379.        
  380.         /// <devdoc>
  381.         /// <para>Writes a category name and the name of the <paramref name="value "/> parameter followed by a line
  382.         /// terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
  383.         /// if a <paramref name="condition"/> is <see langword='true'/>. The
  384.         /// default line terminator is a carriage return followed by a line feed (\r\n).</para>
  385.         /// </devdoc>
  386.         [System.Diagnostics.Conditional("TRACE")]
  387.         public static void WriteLineIf(bool condition, object value, string category)
  388.         {
  389.             TraceInternal.WriteLineIf(condition, value, category);
  390.         }
  391.        
  392.         /// <devdoc>
  393.         /// <para>[To be supplied.]</para>
  394.         /// </devdoc>
  395.         [System.Diagnostics.Conditional("TRACE")]
  396.         public static void Indent()
  397.         {
  398.             TraceInternal.Indent();
  399.         }
  400.        
  401.         /// <devdoc>
  402.         /// <para>[To be supplied.]</para>
  403.         /// </devdoc>
  404.         [System.Diagnostics.Conditional("TRACE")]
  405.         public static void Unindent()
  406.         {
  407.             TraceInternal.Unindent();
  408.         }
  409.     }
  410. }

Developer Fusion