Monday, August 13, 2007

* WCF Diagnostics : Message Logging

Previous Post <<-- WCF Tool : Service Trace Viewer Tool (SvcTraceViewer.exe)

WCF provides sufficient, excellent and configurable out of box configurable message logging facility to monitor and troubleshoot incoming and outgoing messages. This is one of the major improvement over Remoting infrastructure.

  • Message Logging is OFF by default.
  • It provides various levels and options to configure extent of message logging.
  • Service Level : At this level the message is logged when it is about to leave or enter the code. Secure messages are logged decrypted at this level.
  • Transport Level : At this level messages are logged just before getting encoded or after getting decoded for transmission over wire. Even reliable messaging messages are logged.
  • Malformed Level : All the messages which WCF fails to process due to improper format gets logged.
  • Message Filters : Can be applied at Service and Transport. Only messages which match the filter are logged. Filters cannot be applied to Message body.
  • Enabling Message logging for a WCF service involves two modifications to WCF service config file.
  • Adding <diagnostics> section in <system.serviceModel> for setting various levels and options of message logging.
  • Adding <source name="System.ServiceModel.MessageLogging"> in <system.diagnostics> for setting up the listener and logging file.
  • Consider the post "WCF : Monitoring & Troubleshooting using Tracing" . Let's enable message logging also as part of this config file.
  • Add following to <system.serviceModel> section :
          <diagnostics>
    <messageLogging
    logEntireMessage="true"
    logMalformedMessages="true"
    logMessagesAtServiceLevel="true"
    logMessagesAtTransportLevel="true"
    maxMessagesToLog="3000"
    maxSizeOfMessageToLog="2000"/>
    </diagnostics>

  • Add another source to <system.diagnostics> section as following :
    <source name="System.ServiceModel.MessageLogging">
    <listeners>
    <add name="MessageLog"
    type="System.Diagnostics.XmlWriterTraceListener"
    initializeData="D:\Message.svclog" />
    </listeners>
    </source>

  • If you open the above trace file using Service Trace Viewer, You will see multiple following messages 'TransportSend', 'TransportReceive', 'ServiceLevelReceiveRequest' and 'ServiceLevelSendReply'  as we have enabled both service and transport level messages.
  • If the want to see the arguments passed by client and response returned by service, see the following message types : ServiceLevelReceiveRequest - System.ServiceModel.Security.SecurityVerifiedMessage and ServiceLevelSendReply - System.ServiceModel.Dispatcher.OperationFormatter+OperationFormatterMessage

 Next Post -->> WCF : Understanding Data Contracts (Deep Dive)

No comments:

Post a Comment