Tracing in WCF is could be very powerful feature helping to diagnostic activities behind the scene. Unfortunately it is not well documented and not often used at all. In this post I will describe how to use WCF tracing.
Trace in WCF is is build on top of .NET Trace features. It means internally WCF use TraceSource-s. The WCF trace source behind the scene is implemented in the class System.Diagnostics.XmlWriterTraceListener in System assembly. That means it does not have anything to do with WCF. If this is true, then it should be possible to use Microsoft Service Trace Viewer tool without WCF too.
Additionally, it is important to know that WCF use two different sources System.ServiceModel and System.ServiceModelMessageLogging. Both sources are based on XmlWriterListener. That means that even the format of messages in both sources is the same. First one is used for tracing purposes and second one for tracking of messages.
I use usually second one for tracking. If some bad error occurs I usually switch to the first one. Here is the configuration for both. Note that switchValue "Information" is highlighted and no propagateActivity attribute is set (if used this attribute could cause some issues. If you use Service Config Editor this attribute is by default set on true).
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging"
switchValue="Information, ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="c:\\temp\\Web_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
<add initializeData="c:\\temp\\Web_messages.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
If you want to use a trace you do not need any service. Just execute this code below and open c:\\temp\\Web_tracelog.svclog. with WCF Trace Viewer. Picture below shows the result.
Posted
Mar 24 2009, 11:08 PM
by
Damir Dobric