WCF Diagnostics Implementation in 5 Easy Steps

Overview

As we are all aware debugging components in a service oriented architecture it is generally a struggle without having to write a fair amount of code to trace and log the information.  Even using common utilities such as Log4Net or the Enterprise Libraries it still requires you to capture the information you want to have logged.  To my surprise this is no longer the case with Windows Communication Foundation Services Tracing Capabilities.

There are so many features on how to implement WCF debugging, errors logging and tracing that I am only going to focus on a very simple case for the sake of simplicity.  With that said, I am only going to show a few different settings that will establish a level of diagnostics that in most cases should be found very useful.   Especially in cases where you need to show what package and payload information was sent to and received from client’s consuming services.

Let’s get started…..

Step 1: Create a new WCF Service Application

image

 

Step 2:  Open the Web.Config and Add the Following Sections

<system.diagnostics>
    <trace autoflush="true" />
    <sources>
        <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              >
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml"
           type="System.Diagnostics.XmlWriterTraceListener"
  initializeData= "C:\temp\WCFDiagnosticsExample.svclog" />
    </sharedListeners>
  </system.diagnostics>

 

<!--

Add this in your “system.serviceModel” section.

-->

    <diagnostics>
      <messageLogging
            logEntireMessage="true"
            logMalformedMessages="true"
            logMessagesAtServiceLevel="true"
            logMessagesAtTransportLevel="true"
            maxMessagesToLog="3000"
       />
    </diagnostics>

Step 3:  Run the Service

The next step is to simply run your service and you should see the svclog file created in the directory you specified.

Step 4: Open the service log file using the WCF Service Trace Viewer (svctraceviewer.exe)

a ) Launch the WCF Service Trace Viewer (svctraceviewer.exe) as show below

image

b) Next select your log file from the location and path you specified in your web.config.

image

Note:

  • What is great is that you now have the XML data that was sent to your service and the XML that was sent from your service. 
  • You also have the exception that was thrown and any details related to the call stack.  
  • The data that is in the trace also includes your data contracts received and returned with all data captured which is perfect when you have to show what data you received versus what was sent to your service.

Additional Features and Custom Tracing

As easy as it was to enable the tracing you can add your own custom trace as well simply by adding in your own custom trace source and a few lines of code. 

Step 5: Add Custom Trace

a) The quick and simple way to accomplish this is to add your own Custom Trace Source to the diagnostics information.

<source name="CustomTrace"
             switchValue="Information, ActivityTracing"
             >
       <listeners>
         <add name="xml"/>
       </listeners>
     </source>

b) then add the necessary code in your service to log any information needed.

TraceSource traceSource = new TraceSource("CustomTrace");
traceSource.TraceData(TraceEventType.Information, 0, "WCF Diagnostics Trace Example");
traceSource.Flush();

(assumes you added “using System.Diagnostics;” to the top of the class)

Summary

In 5 easy steps we now have enabled logging, tracing, and a custom trace various details about the WCF Service.

References and Links

Service Trace Viewer : http://msdn.microsoft.com/en-us/library/ms732023.aspx

Service Model Registration Tool: http://msdn.microsoft.com/en-us/library/ms732012.aspx

Configuration Editor: http://msdn.microsoft.com/en-us/library/ms732009.aspx

3 Comments

Comments have been disabled for this content.