Tracing and Configuring Asp.Net WCF Services to log messages

Tracing in Asp.Net WCF services enables us to log tracing information and exceptions in a log file on server. System.Diagnostic namespace provides tracing elements, it is same as tracing in Asp.Net Web application.

Steps to follow -
1. Add WCF Trace Sources
2. Add Trace level
3. Configuring Trace Listener
4. Enabling message logging
5. Use Service Trace Viewer to open log file

Add WCF Trace Sources

WCF provides different sources to emit trace information.
We have the following options:


  • System.ServiceModel
  • System.ServiceModel.MessageLogging
  • System.ServiceModel.IdentityModel
  • System.ServiceModel.Activation
  • System.Runtime.Serialization
  • System.IO.Log
  • Cardspace

In the configuration file add source name as:

<source name="System.ServiceModel.MessageLogging">

Add Trace level

Next we are going to add Trace Level, default value is Off.

  1. Off – This will disable message loging.
  2. Critical – add messages for critical event such as application failure.
  3. Error – logs all error and exceptions.
  4. Warning - A condition that may subsequently result in an error or critical failure.
  5. Information -
  6. Verbose – Debug level messages for both user code and http services such a incorrect endpoint.
  7. ActivityTracing - Flow events between processing activities and components. And traces for transfer.
  8. All – will write all information.

<source name="System.ServiceModel.MessageLogging"


Configuring a trace listener
Add listener of type System.Diagnostics.XmlWriterTraceListener. A tracing information will be saved to messages.svclog file in web root directory.


    <add name="messages"


            initializeData="messages.svclog" />

Enabling message logging
Add messageLogging  tag inside diagnostics node in System.ServiceModel to enable message loging.




Message loging attributes

  • logEntireMessage: By default only the message header is logged but if we set it to true, entire message including message header and body will be logged.
  • logMalformedMessages: log messages that are rejected by WCF stack at any stage are known as malformed messages.
  • logMessagesAtServiceLevel: messages those are about to enter or leave user code.
  • logMessagesAtTransportLevel: messages those are about to encode or decode.
  • maxMessagesToLog: maximum quota for messages. Default value is 10000.
  • maxSizeOfMessageToLog: message size in bytes.

Here we have the complete configuration settings.

    <trace autoflush="true"/>
      <source name="System.ServiceModel.MessageLogging">
          <add name="ServiceModelTraceListener">
    <source name="System.ServiceModel" switchValue="Information, ActivityTracing">
          <add name="ServiceModelTraceListener">
      <add initializeData="WebMessages.svclog" type="System.Diagnostics.XmlWriterTraceListener"
        name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
        <filter type="" />
        <messageLogging logEntireMessage="true"

Note: we have set trace autoflush property to true so that messages would not be buffered and will be writen to log as soon as it occurs.

In the above configuration we have added two sources first one is System.ServiceModel.MessageLogging and another is System.ServiceModel namspace for activity tracing. We have also added shared listener with the name ServiceModelTraceListener to be consumed in both the trace sources declared above.

Use Service Trace Viewer to open log file

Microsoft Service Trace Viewer it helps to easily merge, view, and filter trace messages, it helps to diagnose and verify WCF service issues.

Go to start and launch the WCF Service Trace Viewer (svctraceviewer.exe), now select to open svclog log file from the path.

It contains XML data, data that was sent to and from our service, exception details, the data contract we received and returned from our service. 

ServiceTraceViewer.png, Asp.Net, C#, Asp.Net Tracing, WCF,

Back to top