System.Diagnostics.TraceSource Class

Provides a set of methods and properties that enable applications to trace the execution of code and associate trace messages with their source.

See Also: TraceSource Members

Syntax

public class TraceSource

Remarks

The System.Diagnostics.TraceSource class is used by applications to produce traces that can be associated with the application. System.Diagnostics.TraceSource provides tracing methods that allow you to easily trace events, trace data, and issue informational traces. Trace output from System.Diagnostics.TraceSource can be controlled by configuration file settings. The configuration file is located in the folder with the application executable and has the name of the application with the .config file name extension added. For example, the name of the configuration file for TraceSourceSample.exe is TraceSourceSample.exe.config. The configuration file can be used to determine where the trace information is to be sent and what levels of activity are to be traced. The following example shows the contents of a sample application configuration file.

Example

<configuration>
  <system.diagnostics>
    <sources>
      <source name="TraceTest" switchName="SourceSwitch" 
        switchType="System.Diagnostics.SourceSwitch" >
        <listeners>
          <add name="console" />
          <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>
    <sharedListeners>
      <add name="console" 
        type="System.Diagnostics.ConsoleTraceListener" 
        initializeData="false"/>
    </sharedListeners>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="console" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

The System.Diagnostics.TraceSource class is identified by the name of a source, typically the name of the application. The trace messages coming from a particular component can be initiated by a particular trace source, allowing all messages coming from that component to be easily identified.

System.Diagnostics.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.

Note:

You should not call the tracing methods during finalization. Doing so can result in an ObjectDisposedException being thrown.

You can customize the tracing output's target by adding or removing System.Diagnostics.TraceListener instances to or from the collection stored in the TraceSource.Listeners property. By default, trace output is produced using an instance of the System.Diagnostics.DefaultTraceListener class. The preceding configuration file example demonstrates removing the System.Diagnostics.DefaultTraceListener and adding a System.Diagnostics.ConsoleTraceListener to produce the trace output for the trace source. For more information, see <listeners> Element for <source> and <sharedListeners> Element.

Note:

Adding a trace listener to the TraceSource.Listeners collection can cause an exception to be thrown while tracing, if a resource used by the trace listener is not available. The conditions and the exception thrown depend on the trace listener and cannot be enumerated in this topic. It may be useful to place calls to the System.Diagnostics.TraceSource methods in try/catch blocks to detect and handle any exceptions from trace listeners.

The System.Diagnostics.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 System.Diagnostics.Switch and How to: Configure Trace Switches.

Note:

If you modify a configuration file while an application is executing, the application must be stopped and restarted or the Trace.Refresh method must be called before the new settings take effect.

The System.Diagnostics.TraceEventType enumeration is used to define the event type of the trace message. Trace filters use the System.Diagnostics.TraceEventType to determine if a trace listener should produce the trace message.

The trace listeners can optionally have an additional layer of filtering through a trace filter. If a trace listener has an associated filter, the listener calls the TraceFilter.ShouldTrace(TraceEventCache, string, TraceEventType, int, string, Object[], object, Object[]) method on that filter to determine whether or not to produce the trace information.

The trace listeners use the values of the System.Diagnostics.Trace class properties Trace.Indent, Trace.IndentSize, and Trace.AutoFlush to format trace output. You can use configuration file attributes to set the Trace.Indent, Trace.IndentSize, and Trace.AutoFlush properties. The following example sets the Trace.AutoFlush property to false and the Trace.IndentSize property to 3.

Example

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="3" />
  </system.diagnostics>
</configuration>

Requirements

Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0