Using Filters for Message Logging

This sample demonstrates how to create filters for message logging. Only messages that fit the filter criteria are logged, thus reducing unnecessary logging output and making debugging easier. Specifically, this sample demonstrates how to filter based on the SOAP Action element.

NoteNote:

This sample does not include code. You can add the configuration provided in this sample to the configuration file of your application and observe the changes to message logging in the context of your scenario.

Filtering in Message Logging

Windows Communication Foundation (WCF) message logging can be customized using an XPath filter. Only messages that pass through the filter are logged. The filter is defined using XPath language, and can be added to the <messageLogging> element of the configuration file.

The filter lists the conditions a message has to meet to be logged. This example demonstrates how to log messages with a specific soap Action.

Filtering based on Action

Messages may not necessarily contain endpoint or contract references, unless the service does not configure custom Action for the operations.

If custom actions are not used, the Action included in each message takes the following format.

ContractNamespace/ContractName/OperationName

ContractNamespace includes http://.

If you use a SOAP 1.2 version envelope for the message, messages targeted at the contract MyContractName with namespace MyContractNamespace can be filtered for logging, as demonstrated in the following configuration.

<messageLogging logMessagesAtTransportLevel="true" logMessagesAtServiceLevel="true" >
   <filters>
      <add xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">/soap:Envelope/soap:Header/a:Action[starts-with(text(),'MyContractNamespace/MyContractName')]</add>
   </filters>
</messageLogging>

The following is an example of adding a filter specifying the namespace and contract name for the Add request of the Tracing and Message Logging sample.

<filters>
   <add xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">/soap:Envelope/soap:Header/a:Action[starts-with(text(),'http://Microsoft.ServiceModel.Samples/ICalculator/Add')]</add>
</filters>

If custom actions are used, message logging can be used to filter messages using the following action.

<add xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">/soap:Envelope/soap:Header/a:Action/text()="MyAction"</add>

For SOAP 1.1 version envelopes, the SOAP namespace should be replaced with https://schemas.xmlsoap.org/soap/envelope/. You should perform similar changes for different versions of addressing.

For HTTP basic profile, headers are not part of the envelope, and are sent as HTTP headers. You should modify the filtering technique as appropriate.

For more information about Message Logging, please see the Configuring Message Logging topic.

To set up, build, and run the sample

  1. To run this sample, add one of the configuration sections listed previously to the configuration file of your application. Because settings for message logging are valid in the whole application domain, the logging filter affects all messages logged in the application domain.

  2. Run the application. Notice that only messages that satisfy the filter condition are logged to disk.

See Also

Other Resources

Configuring Message Logging

Footer image

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.