Using a Custom Trace Source

A DCS service can generate trace information by using the Microsoft.ConnectedIndustry.ServiceModel.Common.Tracer class in the Microsoft.ConnectedIndustry.ServiceModel.Common assembly. The Tracer class implements the static methods and property described in the following table.

Method/Property

Description

DefaultLogDirectoryCleanup

This method clears existing files with the .LOG file extension from the default folder used by DCS to hold trace information.

SerializeException

This method takes an Exception object and serializes it as a string. The value of this string is returned.

Write

This is an overloaded method that writes a trace message to the event source associated with the Tracer class. You specify an event type from the TraceEventType enumeration (Critical, Error, Information, Resume, Start, Stop, Suspend, Transfer, Verbose, Warning), and the text of the message. You can optionally include the module name.

WriteException

This is an overloaded method that serializes an exception as a string and writes it to the event source associated with the Tracer class. You specify an event type from the TraceEventType enumeration (Critical, Error, Information, Resume, Start, Stop, Suspend, Transfer, Verbose, Warning), the exception, the DCS context, and the module name. You can optionally include the class name, method name, assembly name, application domain, process name, machine name, and additional error message.

Source

This is a property that enables you to get or set the name of the trace source used by the Tracer class. The trace source itself is defined in the <system.diagnostics> section of the configuration file for the service. You can use this property to write to a custom trace source, separate from the CIS and Microsoft.ConnectedIndustry.ServiceModel sources.

The following code example shows how to use the Source property and the Write method of the Tracer class. The code sends the trace output to a trace source called CustomOutput.

...
string source = Tracer.Source;   // Preserve the current trace event source
Tracer.Source = "CustomOutput";
Tracer.Write(System.Diagnostics.TraceEventType.Information, "Output message to custom trace source");
...
Tracer.Source = source;          // Restore the trace event source

Note

If you are writing to a custom trace source, it is important to save the existing trace source and restore it when you have finished, otherwise subsequent trace messages intended for other trace sources might be directed at your trace source instead.

You can configure the CustomOutput trace source in the configuration file for the service. The following example shows how to create the CustomOutput trace source and associate it with a listener. The listener can be any available trace listener, such as the RollFileListener class.

See Also

The RollFileListener Trace Listener Class