Tracing in WCF

Introduction: Tracing in .NET Framework 4.0 and how it is used in WCF Services.

In this article, we will look at how we can trace and debug information in WCF services.Windows Communication Foundation (WCF) provides application instrumentation and diagnostic data for fault monitoring and analysis. Instead of using debugger, You can use tracing to understand how an application is behaving,

WCF Tracing can track all the events or only specified events in the program. Tracing is built upon the System.Diagnostics namespace of the .NET Framework.

WCF outputs the following data for diagnostic tracing:

  • Traces for process milestones across all components of the applications, such as operation calls, code exceptions, warnings and other significant processing events."
  • Windows error events when the tracing feature malfunctions.

The important thing about tracing is:-

It is turned off by default, so it needs to be enable for starting tracing.

There are two ways to enable tracing. These are followings:-

  1. One is through code.
  2. Second is, through config file.

Enabling tracing in config file is good method since we can easily turn it on and off as required. When debugging  is  no longer required we should turn tracing enable off.

Enabling Tracing :

The following code will enable tracing through configuration file:

<
system.diagnostics>
  <trace autoflush="true" />
  <sources>
    <source name="System.ServiceModel"
    switchValue="All">
      <listeners>
         <add name="TraceListeners"
        type="System.Diagnostics.XmlWriterTraceListener"
        initializeData= "c:\Log\trace.svclog"/>
      </listeners>
    </source>
  </
sources
</
system.diagnostics>

Tracing Levels:

The tracing levels are controlled by the switchValue settings. The tracing levels are followings:-


Tracing Level

Description

Off Ignore all trace messages
Critical Log unexpected processing events or unhandled exceptions have occurred. The application will terminate immediately or shortly.
Error An unexpected processing event or exception has occurred. The application was not able to perform a task as expected.
Warnings Indicates there is a possible problem but the application will functioning correctly.
Information Application is running smoothly only that informative message is recorded. In this case, messages are just milestones.
Verbose Very similar to information but provides more details as compared.
ActivityTracing In this case, messages are related to communication between components and the activities.
All In this case, all messages are captured.