Tracing WCF Activity

In situations where we are setting up WCF clients calling a WCF service and are having problems getting the service call to work it is helpful to be able to see a trace of the communication. In order to do this add the below to the .config file of the service and a trace file named, ‘wcfDetailTrace.svcLog’ will be written out to the root folder of the service.

<system.diagnostics>
      <trace autoflush="true">
          <listeners>
          </listeners>
      </trace>
      <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true">

    <listeners>
                   <add name="sdt"
                        type="System.Diagnostics.XmlWriterTraceListener"
                        initializeData= "WcfDetailTrace.svclog" />
               </listeners>
          </source>
      </sources>
 </system.diagnostics>

You can change the location and name of the trace file by changing hte ‘initializeData’ element of the ‘listener’. The trace file is verbose xml that can be parsed by the svcTrace.exe utitlity that comes with Visual Studio. If the svcTrace.exe utility is on your machine the extension .svclog will be mapped to it so double clicking on the trace file will open the trace file in svcTrace.exe.

Here is more information regarding using svctrace: http://msdn.microsoft.com/en-us/library/ms732023.aspx

If you want to log the content of each message then add the ‘MessageLogging’ elment to diagnostics underneath system.serviceModel as shown below to your config file. You will also have to add a source element for message logging to your sources element referencing “System.ServiceModel.MessageLogging”.

The source element for basic tracing only references ”System.ServiceModel” and won’t provide detailed information about message content. Also if tracing messages it looks like the listener has to be given an absolute path instead of relative to the service the trace is logging.

<system.diagnostics>
  <sources>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
                 <add name="messages"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="c:\logs\messages.svclog" />
          </listeners>
      </source>
    </sources>
</system.diagnostics>

<system.serviceModel>
  <diagnostics>
    <messageLogging
         logEntireMessage="true"
         logMalformedMessages="false"
         logMessagesAtServiceLevel="true"
         logMessagesAtTransportLevel="false"
         maxMessagesToLog="3000"
         maxSizeOfMessageToLog="2000"/>
  </diagnostics>
</system.serviceModel>

This should give you message content as well as basic communicatin information. Here is a link to more information about message logging: http://msdn.microsoft.com/en-us/library/ms730064.aspx

svcTrace is pretty useful, output from a trace looks like the below. In the below example it helped us to see we were having a problem with message size.

svctrace.exe

Leave a Reply

Your email address will not be published. Required fields are marked *