Tracing in WCF: Understanding Basic Steps


The theory behind WCF Tracing is essentially the same as a classical definition of tracing. Through tracing an application provides information about itself. Information may vary from the internal state of an object to passed input parameters to a method. This information should be logged, persisted or saved somewhere.

WCF Tracing can be used for the instrumentation of the WCF Service.

There are essentially four steps involved in WCF Tracing:

WCFTrack1.gif

Emitting Trace information from Service

To emit trace information WCF provides different sources. However you have the option to create a trace source using the TraceSource class as well. You can choose any WCF Assembly level trace source to emit the tracing information.

WCF Assembly level Trace Sources are as below:

  1. System.ServiceModel
  2. System.ServiceModel.MessageLogging
  3. System.ServiceModel.IdentityModel
  4. System.ServiceModel.Activation
  5. System.Runtime.Serilization
  6. System.IO.Log

Setting the Trace level

You can define the level of messages you want to trace. If you are tracing all level of messages then the size of the message generated for tracing would be very bulky and it may affect application negatively.

WCF supports the following tracing levels:

WCFTrack2.gif

Trace level information is set by using the switchValue attribute for trace source.

Configuring the listener

You need to configure a listener to persist the messages at any desired location. There must be at least one listener configured for WCF tracing.

The listener can be configured either:
  1. Through code
  2. Or in configuration file.

You can configure a listener directly in the source element of the configuration.

Note: In a further article, I will discuss in detail the configuration of the listener.

Enabling Message logging

The last step you need to perform is to enable message logging in the configuration. WCF logs message at two levels.

WCFTrack3.gif

Different attributes of Message loggings are as below:

WCFTrack4.gif

Now let us go ahead and capture a basic trace in WCF service.
  1. Create a basic WCF Service
  2. Consume service in a client. I am assuming that you are consuming service in a console client.
  3. Open App.config of console client

Emit Trace Information

Add a source in System.Diagostics:

WCFTrack5.gif

We are enabling tracing for assembly level System.ServiceModel.

Set the Trace Level


WCFTrack6.gif

We are setting here trace level to Information and Activity tracing.

Configure the Listener

WCFTrack7.gif

We are using as the listener System.Diagnostics.XmlWriterTraceListener. The trace information would be found in the Traces.svclog file under bin/debug folder.

Trace information gets buffered, so to make it visible you need to flush it using the following configuration:

WCFTrack8.gif

Enable Message Logging

The last step you need to perform is enabling Message logging. Add the following diagnostic in System.ServiceModel.

WCFTrack9.gif

Eventually client side App.Config should look more or less like below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <
system.serviceModel>
      <
diagnostics>
        <
messageLogging logEntireMessage="true"
                        logMalformedMessages="false"
                        logMessagesAtServiceLevel="false"
                        logMessagesAtTransportLevel="true"
                        maxMessagesToLog="3000"
                        maxSizeOfMessageToLog="2000"/>
      </diagnostics>
        <
bindings>
            <
wsHttpBinding>
                <
binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"

                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </
binding>
            </
wsHttpBinding>
        </
bindings>
        <
client>
            <
endpoint address="http://localhost:2934/Service1.svc" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="WSHttpBinding_IService1">
                <identity>
                    <
dns value="localhost" />
                </identity>
            </
endpoint>
        </
client>
    </
system.serviceModel>
  <
system.diagnostics>
    <
sources>
      <
source name="System.ServiceModel.MessageLogging"
              switchValue="Information, ActivityTracing">
        <listeners>
          <
add name="log"
               type="System.Diagnostics.XmlWriterTraceListener"
               initializeData="Traces.svclog"/>
        </listeners>
      </
source>
    </
sources>
    <
trace autoflush ="true"/>
  </system.diagnostics>
</configuration>

After configuring as described above when you run the client application you will get a file in:

Bin\debug\Traces.svclog

Open that file to view the trace.

I hope this post was useful. In a future article I will take you deeper into tracing. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all