WCF logging and tracing with SmartInspect

Distributed software applications are usually difficult to debug. Bugs and performance issues can occur on the client, on the server or in the communication stack. Finding the root cause of such problems is often not trivial, especially if the issue only occurs on a production system. Luckily, .NET logging tools like SmartInspect help with this and allow you to trace method execution end-to-end and log critical application data to analyze the inner-workings of your programs.

The Windows Communication Foundation (WCF) is Microsoft’s approach to service-oriented architectures and allows you to easily built client/server and distributed applications with .NET. SmartInspect can be used to debug applications that make use of WCF and this article (and the attached demo project) explains how to do this. The demo application is based on Microsoft’s WCF tutorial, so if you haven’t worked with WCF before, the tutorial is a good way to start.

The service application

The service application that is part of the demo project exposes a simple service that offers various calculation methods (add, subtract, multiply, and divide). We use SmartInspect to log the method execution, parameters and results:

[sourcecode language="csharp"]
class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
SiAuto.Main.EnterMethod(this, "Add({0},{1})",
new object[] {n1, n2});
double result = n1 + n2;
SiAuto.Main.LogValue("Result", result);
SiAuto.Main.LeaveMethod(this, "Add");
return result;
}

// [..]
}
[/sourcecode]

The client application

The client application connects to the service and uses the exposed methods to calculate various results. The client application is also instrumented with SmartInspect log calls and logs the calculation results returned from service:

[sourcecode language="csharp"]
static void Calculate(CalculatorClient client)
{
SiAuto.Main.EnterMethod("Program.Calculate");

// Call the Add service operation
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
SiAuto.Main.LogMessage("Add({0},{1}) = {2}", value1, value2, result);

// [..]

SiAuto.Main.LeaveMethod("Program.Calculate");
}
[/sourcecode]

Debugging with the SmartInspect Console

When you launch both demo applications, the SmartInspect Console automatically receives the log messages via named-pipes (make sure to start Visual Studio as administrator, as this is required by WCF to register the service). You can also redirect the logging data to log files or other protocols by setting the connection string accordingly.

The SmartInspect Console makes it easy to see the log messages and data in context or to separate the log messages of the client and service. The Console also helps with tracking the involved threads and processes (e.g. you can see in the screenshot that .NET starts and recycles two threads to serve the four service calls).


Log messages of the client and service in the SmartInspect Console

Tracing WCF

WCF also has some tracing and logging capabilities of its own. This allows you to trace the messages and packets WCF sends and receives. Inspecting the exchanged messages is especially useful if you need to debug interoperability issues with other platforms and frameworks. WCF uses .NET’s built-in tracing API to log messages and packets.

Fortunately, SmartInspect makes it easy to receive .NET debug messages as it comes with a custom trace listener. To redirect WCF diagnostic messages to SmartInspect, simply add the Gurock.SmartInspect.DebugTrace assembly as a reference to your project and add the following code to the App.config (the demo projects already come with all the required bits pre-configured; you just need to uncomment the config settings):

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" >
<listeners>
<clear />
<add name="SmartInspect"
type="Gurock.SmartInspect.DebugTrace.SmartInspectTraceListener, Gurock.SmartInspect.DebugTrace" />
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>
</system.serviceModel>
</configuration>
[/sourcecode]

The full demo application will be included in the next SmartInspect release, but you can also download a copy of the demo project from this site in the meantime.

This entry was posted in Gurock Software, Programming, SmartInspect and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.

2 Comments

  1. Shane
    Posted November 1, 2010 at 14:47 | Permalink

    Thanks – this is just what I need: nice article!

  2. Posted April 1, 2012 at 08:53 | Permalink

    Thanks Dennis for this info…it was just what I needed to hand on to my dev partner…we’ve been plagued by a nasty little bug in our call logging software for ages, and your info has really helped us out.

One Trackback

  1. By New SmartInspect demos on June 4, 2010 at 15:23

    [...] Our products, programming & business. « Aspect-oriented logging for .NET with Unity and SmartInspect WCF logging and tracing with SmartInspect » [...]