Theory behind WCF Tracing is not an exception of classical definition of Tracing. Through Tracing an application provides information about itself. Information may vary from internal state of object to passed input parameter to 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

image

Emitting Trace information from Service

To emit trace information WCF provides different sources. However you have choice to create trace source using TraceSource class as well. You can choose any of 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 size of message generated for tracing would be very bulky and it may affect application negatively.

WCF supports below tracing levels

image

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.

Listener can be configured either

  1. Through code
  2. Or in configuration file.

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

Note: In further post, I will discuss in detail on configuring the listener.

Enabling Message logging

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

image

Different attributes of Message loggings are as below,

image

Enough of theory Smile Now let us go ahead and capture 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

image

Set the Trace Level

image

We are setting here trace level to Information and Activity tracing

Configure the Listener

image

We are using listener System.Diagnostics.XmlWriterTraceListener. Trace information would be find at Traces.svclog file under bin/debug folder .

Trace information gets buffered, so to make it visible you need to flush it using below configuration

image

After performing above three steps your System.Diagnostics should look like below,

image

Enable Message Logging

Last step you need to perform is enabling Message logging. Add below diagnostic in System.ServiceModel.

image

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


<?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 said above when you run client application you will get a file in

Bin\debug\Traces.svclog

Open above file to view the trace Smile I hope this post was useful. In further post I will take you to dipper in tracing. Thanks for reading Smile

 

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Advertisements

3 thoughts on “Tracing in WCF: Understanding basic steps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s