Mono Class Library: System.Diagnostics NamespaceSystem.Diagnostics.TraceListener Class |
See Also: TraceListener Members
System.Object
System.MarshalByRefObject
System.Diagnostics.TraceListener
|
Instances of the TraceListener class are used by the System.Diagnostics.Debug and System.Diagnostics.Trace classes for handling diagnostic (trace) messages.
TraceListeners can be managed either through application code, or through the application .config file, by using the (XPath) /configuration/system.diagnostics/trace/listeners and related elements.
To manage TraceListeners in the .config file:
- To add a TraceListener, use the (XPath) /configuration/system.diagnostics/trace/listeners/add element, setting the name, type, and (optional) initializeData attributes. The name attribute sets the TraceListener.Name property, and allows the TraceListener to be removed by-name. The type attribute is the type to instantiate, through Activator.CreateInstance. The initializeData attribute is a string to pass to the type's constructor; it's meaning is dependent upon the type instantiated.
- To remove a TraceListener, use the (XPath) /configuration/system.diagnostics/trace/listeners/remove element, setting the name attribute to the name of the TraceListener to remove.
- To remove all TraceListeners, use the (XPath) /configuration/system.diagnostics/trace/listeners/clear element.
The application .config file can also be used to set the default values of the Trace.AutoFlush and Trace.IndentSize properties on the System.Diagnostics.Debug and System.Diagnostics.Trace classes, by setting the autoflush and indentsize attributes on the (XPath) /configuration/system.diagnostics/trace element.
A sample .config file setting all of these elements:
Application .config file Example <configuration> <system.diagnostics> <!-- Turns off System.Diagnostics.Trace.AutoFlush and sets System.Diagnostics.Trace.TraceListener.IndentSize to 8. --> <trace autoflush="false" indentsize="8"> <listeners> <!-- Add the "foo" TraceListener --> <add name="foo" type="System.Diagnostics.TextWriterTraceListener, System" initializeData="some-file.txt"/> <!-- Remove the TraceListener we just added --> <remove name="foo"/> <!-- Remove all TraceListeners. This also removes the DefaultTraceListener --> <clear/> <!-- Add the DefaultTraceListener back. --> <add name="Default" type="System.Diagnostics.DefaultTraceListener, System"/> </listeners> </trace> </system.diagnostics> </configuration>Note: The string specified in the type attribute will need to be a fully-qualified class name to operate properly with shared assemblies. More specifically, "System.Diagnostics.TextWriterTraceListener, System" will not work on .NET, as that is the fully-qualified class name for a private assembly, not the public/shared assemby part of .NET. Instead, .NET requires "System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".[Edit]
Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0, 2.0.0.0