How to Enable WCF Tracing and WCF Message Logging

This article describes how to use WCF tracing and message logging with various options. With WCF tracing from System.Diagnostics you can configure trace source, emit traces, activity tracing, and its propagation.

WCF Tracing:

WCF tracing enables you to diagnose data, fault messages, and its analysis. Tracing is a better option than debugging to understand the application's behavior and its flow. Tracing gives you the details of all application components like fault and code exceptions, system events, operation calls. Depending on the Switch Value traces will be generated.

WCF tracing is not enabled by default you need to configure it with the switch value and tracelistener.

Switch Value: Switch value decides what level of trace needs to be generated. Below default values are available for Switch Value

  1. Off: Tracing is disabled and no trace will be generated.
  2. Critical: Trace will be generated for unhandled exceptions.
  3. Error: Trace will be generated for all exceptions.
  4. Warning: Traces all possible problems that are occurred or the possibility of occurring. Recommended for production environment with propagateActivity="true"
  5. Information: Only for the successful execution of milestones.
  6. Verbose: Tracing more detail of successful execution. Recommended for debugging purposes.
  7. ActivityTracing: Traces communication across components of the entire application.
  8. All: Traces everything which can be done by above trace levels.

Trace Listener: WCF services and clients add trace data to listeners configured app.config file. There are a couple of predefined trace listeners like XmlWriterTraceListener which can be used to save trace. The initializeData property must be set to some physical file.

Steps to configure WCF Tracing

  1. Create basic WCF service

    Go through the article for creating a service library Create new WCF service library and test using WCFTestClient.

  2. Host WCF Service library

    Host Service as suggested in Hosting WCF service in IIS. or hosting in Windows service

  3. Configure Tracing

    Open the app.config file of the WCF service library and add the configuration for system.diagnostics.

                
      <system.diagnostics>
      <sources>
          <source name="System.ServiceModel" 
                switchValue="Critical,Information,ActivityTracing"
                    propagateActivity="true">
            <listeners>
                     <add name="messages"
                     type="System.Diagnostics.XmlWriterTraceListener"
                     initializeData="c:\logs\messages.svclog" />
              </listeners>
          </source>
        </sources>
        <trace autoflush="true" />
      </system.diagnostics>
                        
                

    In this configuration we are using "Critical, Information, ActivityTracing" as switch value. This will generate traces for all unhandled exceptions, successful execution and communication between two components.

    XmlWriterTraceListener will create the traces in XML format. The property value of initializeData will mention the physical location of the trace file. <trace autoflush="true" /> will flush the traces to file automatically.

  4. Use of TraceViewer

    Execute some of the service operations using client application. The file messages.svclog must have created under C:\logs folder. Open the file by using SvcTraceViewer.exe.

Configure Message Logging

Message logging allows you to log the entire request and response message to log files. For enabling message logging you need to use trace listener System.ServiceModel.MessageLogging and properties of <messagelogging>

System.ServiceModel.MessageLogging has below attributes :
  1. logEntireMessage: boolean property with the default value of false. If set to true entire message that is message header and body will be logged other wise only message header will be logged.
  2. logMalformedMessages: boolean property with the default value of false indicates whether to malformed messages needs to be logged.
  3. logMessagesAtServiceLevel: boolean property with the default value of false indicates whether messages get traced at service level.
  4. logMessagesAtTransportLevel: boolean property with the default value of false indicates whether messages get traced at transport level.
  5. maxMessagesToLog: integer property with the default value of 1000. This property specifies a maximum number of messages can be logged. Malformed messages are not considered for this.
  6. maxSizeOfMessageToLog: integer property with the default value of 262144. The value of this property indicates the maximum size of message to log. Messages more than size will not log.

Add below message logging configuration to .config file.

    
    <system.diagnostics>
        <sources>
            <source name="System.ServiceModel.MessageLogging">
            <listeners>
                    <add name="messages"
                    type="System.Diagnostics.XmlWriterTraceListener"
                    initializeData="c:\logs\northwindservices.svclog" />
                </listeners>
            </source>
        </sources>
    </system.diagnostics>

    <system.serviceModel>
        <diagnostics>
            <messageLogging 
                    logEntireMessage="false" 
                    logMalformedMessages="true"
                    logMessagesAtServiceLevel="true" 
                    logMessagesAtTransportLevel="false"
                    maxMessagesToLog="500"
                    maxSizeOfMessageToLog="5000"/>
        </diagnostics>
    </system.serviceModel>
            
    

Speak your mind
Please login to post your comment!


Blog Search





If you like my content please feel free to buy me coffee. Buy Me A Coffee