Thursday, August 09, 2007

* WCF : Monitoring & Troubleshooting using Tracing

Previous Post <<- WCF : Diagnostics Features

  • Tracing by default is OFF.
  • Provides traces for operation calls, code exceptions, warnings and other important processing events
  • The main elements of Tracing functionality are following :
    • Trace Sources : The places from where trace messages get originated. WCF defines a trace source for each assembly. Some of the trace sources defined by WCF are :
      • System.ServiceModel : Logs all stages of WCF processing.
      • System.ServiceModel.MessageLogging: Logs all messages that flow through the system.
    • Trace Listeners : Traces generated by sources are consumed by listeners for further processing and logging. Listeners provided by System.Diagnostics can be used e.g. XMLWriterTraceListener.
    • Trace Level : controlled by the switchValue setting of the trace source, some of the valid values are : Off, Critical, Error, Warning, Information, All, etc.
  • Let's start from our last code tutorial where we hosted our basic MathService on IIS and created a console based client.
  • This is our original web.config file :
    <configuration>
    <system.serviceModel>
    <services>
    <service name="MathUtility.MathService" behaviorConfiguration="MathServiceBehavior">
    <endpoint address="" binding="wsHttpBinding" contract="MathUtility.IMath">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
    </endpoint>
    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
    <behavior name="MathServiceBehavior">
    <serviceMetadata httpGetEnabled="True" />
    </behavior>
    </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
    </configuration>

  • Add system.diagnostics section to this file as following :
    <configuration>
    <system.serviceModel>
    <services>
    <service name="MathUtility.MathService" behaviorConfiguration="MathServiceBehavior">
    <endpoint address="" binding="wsHttpBinding" contract="MathUtility.IMath">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
    </endpoint>
    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
    <behavior name="MathServiceBehavior">
    <serviceMetadata httpGetEnabled="True" />
    </behavior>
    </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
    <!-- Configuration for setting up Tracing Functionality -->
    <system.diagnostics>
    <trace autoflush="true" />
    <sources>
    <!-- Set the trace source & trace level-->
    <source name="System.ServiceModel"
    switchValue="All"
    >
    <!-- set the trace listener -->
    <listeners>
    <add name="traceListener"
    type="System.Diagnostics.XmlWriterTraceListener"
    initializeData= "D:\Traces.svclog" />
    </listeners>
    </source>
    </sources>
    </system.diagnostics>
    </configuration>

  • Run the client again and you will see Traces.svclog file getting generated. As we have set the Trace level as 'All' the iformation contained in this file will be large. It is an xml file but not well formed and so you cannot open it in XML Notepad. You can open it in Notepad or a tool called 'Service Trace Viewer Tool' which I will cover next.
  • Same kind of section can be added in client app.config also to generate traces at client end.

Next Post ->> WCF Tool : Service Trace Viewer Tool (SvcTraceViewer.exe)

No comments:

Post a Comment