Using the Microsoft Service Trace Viewer

24 August 2011

Debugging WCF client/service interactions can be hard. In a recent post on WCF RESTful services I reviewed how useful Fiddler2 can be. However, for a really flexible and comprehensive approach, the Microsoft Service Trace Viewer (STV hereafter) is hard to beat.

The STV is a desktop utility that reads, filters and provides various customizable views of the contents of log files generated by WCF clients and services.

Configuration

In summary, you:

  • Through the web.config or app.config file, configure one or more sources of diagnostic trace messages in your client and/or service
  • Configure a listener to capture the diagnostic traces
  • Initiate a client/service interaction as normal
  • Run the STV and open one or more of the logs created by the trace sources

As a practical example, we'll review a very simple 'Hello' IIS/AppFabric-hosted WCF service (WcfSimpleTextService) with a WPF client (WcfSimpleTextServiceClient).

The configuration is exactly the same (apart from perhaps the name of the log file) in both the client's app.config file and service's web.config file. Here's how to configure the client/service to show 'everything' (ultra-verbose):

  <?xml version="1.0"?>
  <configuration>
    <system.diagnostics>
  
      <sources>
        <source name="System.ServiceModel" propagateActivity="true" switchValue="All">
          <listeners>
            <add name="xmlTraceListener" />
          </listeners>
        </source>
        <source name="System.ServiceModel.MessageLogging" switchValue="All">
          <listeners>
            <add name="xmlTraceListener" />
          </listeners>
        </source>
      </sources>
  
      <sharedListeners>
        <add name="xmlTraceListener"
             type="System.Diagnostics.XmlWriterTraceListener"
             initializeData="C:\WcfLogs\Service.svclog" />
      </sharedListeners>
    </system.diagnostics>
    :
    :
    <system.serviceModel>

      <diagnostics>
        <messageLogging logEntireMessage="true" 
                        logMalformedMessages="true" 
                        logMessagesAtServiceLevel="true" 
                        logMessagesAtTransportLevel="true">
          <filters>
            <clear/>
          </filters>
        </messageLogging>
      </diagnostics>
      :
      :
  

In the above configuration, the switchValue (which controls how much trace information is emitted by each source) can have any of the following values:

For full details of each value refer to the MSDN documentation.

The only variant between the client and service configuration is the log name specified by initializeData in the <sharedListeners> section.

Note that the account used to run the service must be given write permissions on the directory containing the log. The easiest way to do that is to grant the IIS_IUSRS group modify/write permissions.

Examining Log Files

The STV executable (which is part of the Windows SDK, and not the .NET Framework for some reason) is located by default in: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\SvcTraceViewer.exe

First, open the client log file, then use the File | Add command to add the contents of the service log file:

The Activity tab shows a collection of related messages:

You can then scroll through the messages in that activity in the right-hand pane:

However, the real power of STV is hidden in the Graph tab.

Here we can see how messages flow over time between the client and service (shown as w3wp - the ASP.NET worker process under which the service is running).

For example, the image below shows the service's response to the SayHello() message:

Conclusion

The Microsoft Service Trace Viewer is a great to see what's happening when messages flow between WCF services and clients. However, be aware that emitting trace messages places additional burden on the server. You should follow Microsoft's published best practices when deploying trace-enabled WCF solutions into a production environment.