Logging Full SOAP Messages

Working with SOAP so much often leaves me needing to view the entire SOAP message that is being sent to (or from) my WCF services. For a long while I had the chunk of XML configuration code stored away on my machine knowing that I would have to use it again. Unfortunately, I lost it.

Yesterday, when searching the web for how to get the full SOAP message logged in trace logs I had to go through (in my opinion) too many articles to find what I eventually needed.

So, although this may be repeated information from other posts you may find, it is better to have too much info on the web rather than too little (imho).

To log the entire soap message, received and sent, to and from your WCF service, add this to yor web.config:

<system.diagnostics>
	<sources>
		<source name="System.ServiceModel" switchValue="Verbose,ActivityTracing" propagateActivity="true">
			<listeners>
				<add type="System.Diagnostics.DefaultTraceListener" name="Default">
					<filter type=""/>
				</add>
				<add name="ServiceModelTraceListener">
					<filter type=""/>
				</add>
			</listeners>
		</source>
		<source name="System.ServiceModel.MessageLogging" switchValue="Verbose, ActivityTracing">
			<listeners>
				<add type="System.Diagnostics.DefaultTraceListener" name="Default">
					<filter type=""/>
				</add>
				<add name="ServiceModelMessageLoggingListener">
					<filter type=""/>
				</add>
			</listeners>
		</source>
	</sources>
	<sharedListeners>
		<add initializeData="C:\temp\Web_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
			<filter type=""/>
		</add>
		<add initializeData="C:\temp\Web_messages.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
			<filter type=""/>
		</add>
	</sharedListeners>
	<trace autoflush="true"/>
</system.diagnostics>

Now, note that this should go within the <configuration> element, after the <configSections> element (if you have one, which you likely do).

In addition to the above, you also have to have:

<system.serviceModel>
	<diagnostics>
		<messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"/>
	</diagnostics>
</system.serviceModel>

Without this, you won’t get your full soap messages.

Note: If you are using .svc for a handler, you have the option to specify a Factory class that should be used to initialize the service. If your factory class happens to use a different Web.config file, then you may have to re-arrange the above XML snippets according to the way your other Web.config is setup. In my case, I had a Web.config that defined all the core service definitions in a separate file from the Web.config that defined <system.Web> and custom properties. In that case, I had to have my system.serviceModel/diagnostics chunk in the first Web.config and the system.diagnostics section in the other config.

Hope this helps someone out there.

 

Posted in: IT

Leave a Reply

Your email address will not be published.

Humanity Verification *Captcha loading...