zz - MSDN TraceSource class

来源:互联网 发布:域名抢注程序 编辑:程序博客网 时间:2024/04/18 14:40
in this post, we are going to discuss the TraceSource class, which should be used to enable applicatin to trace the code execution and associate the messages with their sources.

The original of the discussion is spawned from the post here: http://msdn.microsoft.com/en-us/library/system.diagnostics.tracesource.aspx 

This article is not yet completed, please refer to the original article for more details. 


Below is some annotated code which illustrate the API use of the TraceSource classes. 

// The following configuration file can be used with this sample. // When using a configuration file #define ConfigFile. //<configuration> //    <system.diagnostics> //        <sources> //            <source name="TraceTest" switchName="SourceSwitch" switchType="System.Diagnostics.SourceSwitch" >//                <listeners> //                    <add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false" />//                    <remove name ="Default" />//                </listeners> //            </source> //        </sources> //        <switches> //            <!-- You can set the level at which tracing is to occur --> //            <add name="SourceSwitch" value="Warning" />//            <!-- You can turn tracing off --> //            <!--add name="SourceSwitch" value="Off" -->//        </switches> //        <trace autoflush="true" indentsize="4"></trace>//    </system.diagnostics> //</configuration>#define TRACEusing System;using System.Collections;using System.Diagnostics;using System.Reflection;using System.IO;using System.Security.Permissions;namespace TraceSourcesTest{  class Program  {    // NOTE:     // this is a test class that test on the use of the TraceSource classes     //     static TraceSource ts = new TraceSource("TraceTest");    [SwitchAttribute("SourceSwitch", typeof(SourceSwitch))]    static void Main(string[] args)    {      try      {        // Initialize trace switches.#if(!ConfigFile)        SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose");        ts.Switch = sourceSwitch;        int idxConsole = ts.Listeners.Add(new System.Diagnostics.ConsoleTraceListener());        ts.Listeners[idxConsole].Name = "console";#endif        DisplayProperties(ts);        ts.Listeners["console"].TraceOutputOptions |= TraceOptions.Callstack;        ts.TraceEvent(TraceEventType.Warning, 1);        ts.Listeners["console"].TraceOutputOptions = TraceOptions.DateTime;        // Issue file not found message as a warning.        ts.TraceEvent(TraceEventType.Warning, 2, "File Test not found");        // Issue file not found message as a verbose event using a formatted string.        ts.TraceEvent(TraceEventType.Verbose, 3, "File {0} not found.", "test");        // Issue file not found message as information.        ts.TraceInformation("File {0} not found.", "test");        ts.Listeners["console"].TraceOutputOptions |= TraceOptions.LogicalOperationStack;        // Issue file not found message as an error event.        ts.TraceEvent(TraceEventType.Error, 4, "File {0} not found.", "test");        // Test the filter on the ConsoleTraceListener.        ts.Listeners["console"].Filter = new SourceFilter("No match");        ts.TraceData(TraceEventType.Error, 5,            "SourceFilter should reject this message for the console trace listener.");        ts.Listeners["console"].Filter = new SourceFilter("TraceTest");        ts.TraceData(TraceEventType.Error, 6,            "SourceFilter should let this message through on the console trace listener.");        ts.Listeners["console"].Filter = null;        // Use the TraceData method.         ts.TraceData(TraceEventType.Warning, 7, new object());        ts.TraceData(TraceEventType.Warning, 8, new object[] { "Message 1", "Message 2" });        // Activity tests.        ts.TraceEvent(TraceEventType.Start, 9, "Will not appear until the switch is changed.");        ts.Switch.Level = SourceLevels.ActivityTracing | SourceLevels.Critical;        ts.TraceEvent(TraceEventType.Suspend, 10, "Switch includes ActivityTracing, this should appear");        ts.TraceEvent(TraceEventType.Critical, 11, "Switch includes Critical, this should appear");        ts.Flush();        ts.Close();        Console.WriteLine("Press any key to exit.");        Console.Read();      }      catch (Exception e)      {        // Catch any unexpected exception.        Console.WriteLine("Unexpected exception: " + e.ToString());        Console.Read();      }    }    public static void DisplayProperties(TraceSource ts)    {      Console.WriteLine("TraceSource name = " + ts.Name);      Console.WriteLine("TraceSource switch level = " + ts.Switch.Level);      Console.WriteLine("TraceSource switch = " + ts.Switch.DisplayName);      SwitchAttribute[] switches = SwitchAttribute.GetAll(Assembly.GetExecutingAssembly());      for (int i = 0; i < switches.Length; i++)      {        Console.WriteLine("Switch name = " + switches[i].SwitchName);        Console.WriteLine("Switch type = " + switches[i].SwitchType);      }#if(ConfigFile)            // Get the custom attributes for the TraceSource.            Console.WriteLine("Number of custom trace source attributes = "                + ts.Attributes.Count);            foreach (DictionaryEntry de in ts.Attributes)                Console.WriteLine("Custom trace source attribute = "                    + de.Key + "  " + de.Value);            // Get the custom attributes for the trace source switch.             foreach (DictionaryEntry de in ts.Switch.Attributes)                Console.WriteLine("Custom switch attribute = "                    + de.Key + "  " + de.Value);#endif      Console.WriteLine("Number of listeners = " + ts.Listeners.Count);      foreach (TraceListener traceListener in ts.Listeners)      {        Console.Write("TraceListener: " + traceListener.Name + "\t");        // The following output can be used to update the configuration file.        Console.WriteLine("AssemblyQualifiedName = " +            (traceListener.GetType().AssemblyQualifiedName));      }    }  }}

And another example of the use of TraceSource is the Configuration file that it may use, here is the content of one configuration file. 
<?xml version="1.0" encoding="utf-8" ?><configuration>  <!-- boqwang : this secion will contain the Diagnostics section that is enabeld by   the TraceSource class -->  <system.diagnostics>    <sources>      <!-- define a TraceTest trace point, and the swith type is 'System.Diagnostics.SourceSwitch-->      <source name="TraceTest" switchName="SourceSwitch"        switchType="System.Diagnostics.SourceSwitch" >        <listeners>          <add name="console" />          <remove name ="Default" />        </listeners>      </source>    </sources>    <!-- you can separately define tbe switches?? check that you uses the Switch defined below in the above Source "TraceTest"-->    <!-- boqwang: The SourceSwitch class provides the means to dynamically control the tracing output. The preceding configuration file example shows how you can turn off tracing from a trace source and control the level at which tracing occurs. You can modify the value of the source switch without recompiling your application. For information on using the configuration file to set a switch, see Switch and How to: Configure Trace Switches. -->    <switches>      <!-- You can set the level at which tracing is to occur -->      <add name="SourceSwitch" value="Warning" />      <!-- You can turn tracing off -->      <!--add name="SourceSwitch" value="Off" -->    </switches>    <sharedListeners>      <add name="console"           type="System.Diagnostics.ConsoleTraceListener"           initializeData="false" />      <!-- TraceSource defines tracing methods but does not actually provide any specific mechanism for generating and storing tracing data. The tracing data is produced by trace listeners, which are plug-ins that can be loaded by trace sources.  -->    </sharedListeners>    <!-- Option that controls this trace -->    <trace autoflush="true" indentsize="4">      <listeners>        <add name="console" />      </listeners>    </trace>  </system.diagnostics></configuration>