Service Diagnostics: Message Logging

8. March 2007 11:00

Message logging is a valuable diagnostic function built into WCF.  It provides a mechanism to record and view the actual messages exchanged between a service and clients.  This can be rather useful when you have a need to get a look at the physical messages.  It should be emphasized that message logging can be enabled on the client and/or the service. 

Let's take a look at some of the basics.

First, the service configuration must be modified to enable message logging.  This can be done rather easily via the WCF Configuration Editor.  Refer to my previous post for the specifics on how to enable diagnostics.  The most interesting part of the configuration for message logging is shown below:

There are several options available for tweaking the manner in which messages are logged.  Here are a few examples:

  • Log the entire message or only the header
  • Log at the transport level (on the wire)
  • Log at the service level (before message is written to/read from the wire)

If you want to capture the unencrypted values in a message, you should log at the service level.  Depending upon your transport and various settings within the binding, messages logged at the transport level wire may be encrypted or otherwise unreadable. 

Once the service has been configured appropriately, invoke a few operations from a client to record the exchanged messages within the log file.  After the log file has been created, the Service Trace Viewer can be used to examine the results.  This handy little gem is one of my favorite WCF utilities.  It is included with the Windows SDK for Vista and .NET Framework 3.0 Runtime Components (what a mouthful).  It is installed to the following path:

<Root Drive>\Program Files\Microsoft SDKs\Windows\v6.0\Bin\ServiceTraceViewer.exe

It is also accessible via the Start Menu:

Start -> All Programs -> Microsoft Windows SDK -> Tools -> Service Trace Viewer

Personally, I have added the utility to my Visual Studio Tools menu. This makes it very easy to navigate to the application when working within VS.  To include it in your Tools menu, navigate to Tools -> External Tools and configure a menu entry accordingly.

As shown in the image, the Service Trace Viewer provides a nice GUI to read the contents of the message log.  Notice the grid that displays each message recorded in the log.  When you click on an individual message, the message content is displayed in three different tab views:  Formatted, Xml, and Message.

The formatted view is shown in the above image.  It essentially breaks down all of the message content into categories that allow for easier reading via the user interface.  The message in the sceenshot was logged at the transport level using a BasicHttpBinding.  Note the General Message Information section displays the Http headers included with the message.  The Envelope Information section displays the actual Soap headers, method, and parameters.

The Xml tab displays the entire xml node for the specific message in the log file. 

The Message tab displays the raw xml for the actual service message.  If you are logging at the transport level, it will contain transport-specific xml that precedes the Soap Envelope. 

Another feature worth mentioning is the ability to search through the message log for specific text, which can be limited to specific fields within the log.  This functionality can be found in the toolbars just below the menu.

This really just scratches the surface in terms of the Service Trace Viewer capabilities.  However, this should give you an idea of what it can do.  I highly recommend experimenting with various diagnostic configurations and examining the output in the Service Trace Viewer.  Every WCF developer should possess a basic understanding of it.

Comments are closed

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.

Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn


Shared Items


Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.