I'm using WCF (.NET 3.5) to communicate with a server using SOAP. When running in debug mode, I use System.ServiceMode.Dispatcher.IClientMessageInspector
and log4Net to log the request content.
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
log.Debug(request);
}
My difficulty is that sometimes the SOAP message contains authentication information that I must mask before writing to the logs
e.g. in the following example I would like to log the password element as <password>**********</password>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://service.soap.host.com/credentials</Action>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<credentials xmlns="http://service..soap.host.com/credentials">
<username>MyUsername</username>
<password>MyPassword</password>
</credentials>
</s:Body>
</s:Envelope>
I'm able to achieve this in a crude way using regular expression matching on the output of request.ToString()
but I wonder if there's a more elegant and efficient approach that will allow me to modify the value of the password element before converting the message to a string.