Thursday, March 08, 2012

WCF : Message logging


The best developer tool is Google. 99 % of the time you can find an answer or sample code to your problem.

On the other hand, the worst developer tool is Google. At least half the links are rubbish. There should be some kind of peer review like stackoverflow has to delete crap articles from the Web.

This came about because I was looking for a way to log the actual messages being sent over WCF.

Came across loads of articles, none of which worked 100%. Some were misleading and some were absolute rubbish.

The worst aspect was that they include some small XML snippet which you cut and paste into the web.config without any context i.e. just where are you supposed to insert it?

Anyway, here’s the solution I came up with:

<configuration>
...
  </system.serviceModel>
    <diagnostics>
         <messageLogging 
         logEntireMessage="true" 
         logMalformedMessages="false"
         logMessagesAtServiceLevel="true" 
         logMessagesAtTransportLevel="true"
         maxMessagesToLog="3000"
         maxSizeOfMessageToLog="2000"/>
     </diagnostics>    
  </system.serviceModel>
...
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue=
"Information,ActivityTracing"
        propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="messages"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="c:\...\messages.svclog" />
        </listeners>
      </source>  
    </sources>
       <sharedListeners>
      <add initializeData="C:\...\Trace.svclog"  
type="System.Diagnostics.XmlWriterTraceListener"
        name="xml" />
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>
...
<configuration>

Enjoy!

No comments: