Click here to Skip to main content
15,867,879 members
Articles / .NET / .NET4

Simple Steps to Enable Tracing in WCF

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
13 Jul 2012CPOL1 min read 91.3K   9   3
Simple steps to enable tracing in WCF

Introduction

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

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 configuration file, we will define a source to enable this configuration as follows:

    XML
    <source name="System.ServiceModel.MessageLogging">
  2. Setting Tracing Level, we have the following available options, we need to set this tracing level to available options other than default "Off":
    • Off
    • Critical
    • Error
    • Warning
    • Information
    • Verbose
    • ActivityTracing
    • All

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

    XML
    <source name="System.ServiceModel.MessageLogging"
                                   switchValue="Information">
  3. Configuring a trace listener:

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

    XML
    <listeners>
        <add name="messages"
                type="System.Diagnostics.XmlWriterTraceListener"
                initializeData="d:\logs\messages.svclog" />
    </listeners>
  4. Enabling message logging:
    XML
    <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, the entire message including message header as well as body will be logged.
    • logMalformedMessages: This option logs messages that are rejected by WCF stack at any stage known as malformed messages.
    • logMessagesAtServiceLevel: Messages those are about to enter or leave user.
    • logMessagesAtTransportLevel: Messages those are about to encode or decode.
    • maxMessagesToLog: Maximum quota for messages. Default value is 10000.
    • maxSizeOfMessageToLog: Message size in bytes.

Finally, putting all this together, the configuration file will appear like this:

XML
<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 file automatically. So, we can set the autoflush property of the trace under sources as follows:

XML
<trace autoflush ="true" /> 

Other Top WCF Tutorials

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
QuestionProgrammatically Pin
stixoffire22-Sep-15 3:50
stixoffire22-Sep-15 3:50 
QuestionMore options for logging Pin
katrash17-Jul-12 6:33
katrash17-Jul-12 6:33 
You can combine multiple trace sources and get them all log to one file and control indentation level as shown below:
<system.diagnostics>
  <trace autoflush="true" indentsize="4" />
  <sources>
    <source name="System.ServiceModel" switchValue="Warning">
      <listeners>
        <add name="textLogger" />
      </listeners>
    </source>
    <source name="System.ServiceModel.MessageLogging" switchValue="All">
      <listeners>
        <add name="textLogger" />
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add name="textLogger"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="d:\logs\wcf_svclog.txt" />
  </sharedListeners>
</system.diagnostics>

SuggestionFormatting... Pin
Sandeep Mewara12-Jul-12 22:54
mveSandeep Mewara12-Jul-12 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.