WCF: Logging All SOAP Messages

Do you want to know what is actually in that SOAP message that your Windows Communication Foundation service is sending? Well, look no further. It’s actually quite simple to output the XML that is being created.

First, there are a couple classes that need to be added to your service project (or a separate project). The first of these is perhaps the most important, which is the message inspector that is used to do the actual logging, etc.

Next, we’ll create a behavior that uses this inspector.

Now we’ll create a Behavior Extension that uses the behavior we created.

Finally, we have to set up the behavior extension in the web.config so that the service uses it.

1. Assuming that there is already an element in the <system.serviceModel><services>, change the appropriate element to have a behaviorConfiguration=”LoggingBehavior”.

2. Still under <system.serviceModel>, add an element under <behaviorExtensions> with the name “loggingBehavior” and the type pointing to the full namespace of our LoggingBehaviorExtensionElement.

3. Again, still under <system.serviceModel>, add a element under <behaviors>/<endpointBehaviors> with the name “LoggingBehavior” and an element within it called “loggingBehavior”.

Here’s the full XML that we’re adding to the web.config:

That’s all there is to it. You can add code to log using your favorite logger within the LoggingMessageInspector methods. Feel free to comment if you have any questions or improvements.

Leave a Reply

Your email address will not be published. Required fields are marked *