I have an application that uses a ClientBase object generated by a service reference to call a third party WCF SOAP service. Every once in awhile, the service call returns a fault exception instead with an exception message of "An error occurred while processing the request" which is completely generic.
Even during service errors, there is supposed to be a trace ID value that comes back with the response so the developers of that service can debug/troubleshoot any issues. So, what I ideally want is to log the raw response that comes back to my ClientBase object when there is an exception. I don't want to log every single message if possible, that would be overkill IMO.
Is there a way to capture that using the ClientBase? Maybe there's some context object that contains the raw response content? This needs to be built into my application if possible. I know there are tools out there that act as a proxy between the client and service that can log http requests/responses but that is not what I'm after.
At the
ClientBase
itself there's no place where you can get that information. But you can add a custom message inspector at the client (IClientMessageInspector
) where you can see all the messages that are being received; for those messages you can check theIsFault
property, and if true log the message as you want.Update: Adding sample code
You may consider another approach to capture XML - custom MessageEncoder. Unlike
IClientMessageInspector
it handles original byte content of http body.You need to wrap a standard textMessageEncoding with custom message encoder as new binding element and apply that custom binding to endpoint in your config.
Also you can look how I did it in my project - wrapping textMessageEncoding, logging encoder, custom binding element and config.