我正在使用WCF(.NET 3.5)與使用SOAP的服務器進行通信。在調試模式下運行時,我使用System.ServiceMode.Dispatcher.IClientMessageInspector
和log4Net來記錄請求內容。從IClientMessageInspector登錄時屏蔽敏感信息
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
log.Debug(request);
}
我的困難是,有時SOAP消息中包含的認證信息,我必須寫日誌 例如之前面膜在下面的例子中,我想密碼元素數爲<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>
我能使用正則表達式對request.ToString()
輸出匹配粗暴的方式來實現這一點,但我不知道是否有一個更優雅有效的方法,這將允許我在將消息轉換爲字符串之前修改密碼元素的值。