Simple Steps to Enable Tracing in WCF

The tracing mechanism in the Windows Communication Foundation is based on the classes that reside in the System.Diagnostic namespace. The important classes are Trace, TraceSource and TraceListener.

The following are the steps to enable tracing in WCF.

1. Configuring WCF to emit tracing information/Define Trace Source, we have the following options:

  • System.ServiceModel
  • System.ServiceModel.MessageLogging
  • System.ServiceModel.IdentityModel
  • System.ServiceModel.Activation
  • System.Runtime.Serialization
  • System.IO.Log
  • Cardspace

In the configuration file, we will define a source to enable this configuration as follows:

<source name="System.ServiceModel.MessageLogging">

2. For setting the Tracing Level, we have the following available options; we need to set this tracing level to one of the available options other than the default "Off":

  • Off
  • Critical
  • Error
  • Warning
  • Information
  • Verbose
  • ActivityTracing
  • All

In the configuration file, we can choose the above values for the switchValue attribute as follows:

<source name="System.ServiceModel.MessageLogging"
switchValue="Information">

3. Configuring a trace listener.

For configuring a trace listener, we will add the following to the config file:

     <listeners>
                 <add name="messages"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="d:\logs\messages.svclog" />
      </listeners>


4. Enabling message logging:

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


logEntireMessage: By default, only the message header is logged but if we set it to true then the entire message, including message header as well as the body, will be logged.

  • logMalformedMessages: this option logs those messages rejected by the WCF stack at any stage, which are known as malformed messages.
  • logMessagesAtServiceLevel: messages sent soon before entering or leaving user code.
  • logMessagesAtTransportLevel: messages sent about an encode or decode.
  • maxMessagesToLog: maximum quota for messages. The default value is 10000.
  • maxSizeOfMessageToLog: message size in bytes.

Putting all this together, the configuration file will appear like this.

    <system.diagnostics>

    <sources>

      <source name="System.ServiceModel.MessageLogging">

        <listeners>

          <add name="messagelistener"

               type="System.Diagnostics.XmlWriterTraceListener" initializeData="d:\logs\myMessages.svclog"></add>

        </listeners>

      </source>

    </sources>

  </system.diagnostics>

    <system.serviceModel>       

      <diagnostics>

        <messageLogging logEntireMessage="true"

                        logMessagesAtServiceLevel="false"

                        logMessagesAtTransportLevel="false"

                        logMalformedMessages="true"

                        maxMessagesToLog="5000"

                        maxSizeOfMessageToLog="2000">         

        </messageLogging>

      </diagnostics>

    </system.serviceModel>

Note: In this case, information will be buffered and not published to the file automatically, So, we can set the autoflush property of the trace under sources as follows:

<trace autoflush ="true" />


Similar Articles