How to Controll the Content and Origin of Messages in WCF Services?

To generate useful diagnostic information to debug a problem in your WCF solution, you must match the level of detail and the amount of coverage to the problem at hand. If the default values that the message-logging trace source uses do not meet your requirements, you can add a messageLogging element to your WCF configuration file under the diagnostics subelement of the system.ServiceModel element to control how it functions and what information it logs. For example, you can configure the message-logging trace source to record the entire message instead of recording only the message header. The attributes of the messageLogging element are:

  • logEntireMessage. When the value is true, the message header and body are recorded. When the value is false (the default), only the message header is recorded.

  • logMalformedMessages. When the value is true, corrupt or incorrectly formatted messages are recorded. The default value is false.

  • logMessagesAtServiceLevel. When the value is true, messages at the service level are recorded before encryption and transportation. The default value is false.

  • logMessagesAtTransportLevel. When the value is true, messages at the transport level are recorded. The default value is false.

  • maxMessagesToLog. This attribute sets the maximum number of messages to be recorded. The default value is 10,000.

  • maxSizeOfMessageToLog. This attribute sets the maximum size in bytes of the messages to be recorded. The default value is 256k for service and transport-level messages, and 4k for malformed messages.

By default, the message-logging trace source omits any known security information, including keys and personal identifiable information (PII) such as user names and passwords, that may be present. You can override this behavior by using a combination of application-level and machine-level configuration elements.

If you want to record particular messages, you can define an XPath filter to determine which ones are recorded. If there are no filters, the message-logging trace source logs all messages. To configure message filtering, add a filters element to your WCF configuration. Within the filters element, you can add one or more filters and set their attributes, such as the nodeQuota attribute, which limits the size of the XPath Document Object Model (DOM) that is examined when looking to match the filter criterion.

The following code example shows the messageLogging element and associated attributes.

[Message logging example]

  ...

  <system.serviceModel>

    <diagnostics>

      <messageLogging

           logEntireMessage="true"

           logMalformedMessages="false"

           logMessagesAtServiceLevel="true"

           logMessagesAtTransportLevel="false"

           maxMessagesToLog="3000"

           maxSizeOfMessageToLog="2000"/>

    </diagnostics>

  </system.serviceModel>

  ...

The following code examples show the necessary configuration elements at both the application level and the machine level.

  [Application level configuration]

  <system.diagnostics>

    <sources>

      <source name="System.ServiceModel.MessageLogging"

                   logKnownPii="true">

        <listeners>

          <add name="messages"

               type="System.Diagnostics.XmlWriterTraceListener"

               initializeData="c:\\logs\\messages.svclog" />

        </listeners>

      </source>

    </sources>
  </
system.diagnostics>

 

 

  [Machine level configuration]

  <configuration>

    <system.ServiceModel>

      <machineSettings enableLoggingKnownPii="true"/>

    </system.ServiceModel>

  </configuration>

The following code example shows how to enable message filtering.

  [Filtering example]

  <messageLogging

     logEntireMessage="true"

     logMalformedMessages="true"