Sdílet prostřednictvím


Configuring WCF Integration Trace Listeners

Windows Communication Foundation (WCF) is able to log directly only to System.Diagnostics trace sources, so it is necessary to configure a special trace listener named the EntLibLoggingProxyTraceListener in the <system.diagnostics> configuration section to enable the Logging Application Block to process WCF log messages. This trace listener receives messages from the trace source, wraps them in a LogEntry object, and forwards them to the Logging Application Block, where they can be processed according to the Logging Application Block’s configuration. If the original message is in XML format (as is the case when messages are generated from WCF), the EntLibLoggingProxyTraceListener creates an XmlLogEntry object instead of a LogEntry. The XmlLogEntry class is derived from the standard LogEntry class and adds support for an XML payload.

The EntLibLoggingProxyTraceListener will add the name of its containing trace source as a category to each XmlLogEntry it creates. In addition, it is possible to configure the EntLibLoggingProxyTraceListener to extract information from the XML data for use as additional categories. This can be specified using XPath queries in the definition of the trace listener in the configuration file. The categoriesXPathQueries attribute can be set to a semicolon-delimited list of XPath queries, and the namespaces attribute can be set to a space-delimited list of XML namespaces used in the XPath queries, as shown in the following example.

<add name="entlibproxywithmultiplexpaths"
     type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EntLibLoggingProxyTraceListener, 
           Microsoft.Practices.EnterpriseLibrary.Logging"
     categoriesXPathQueries="//MessageLogTraceRecord/@Source;//MessageLogTraceRecord/@Source2"
     namespaces="xmlns:pre='urn:test' xmlns:pre2='urn:test2'"/>

To use the EntLibLoggingProxyTraceListener with WCF, you will need to define a trace source named System.ServiceModel in the <system.diagnostics> configuration section and turn on logging in WCF by specifying appropriate values in the <diagnostics> section in the <system.serviceModel> configuration section. Note that the Enterprise Library configuration tools do not support editing either of these sections, so you must use a text editor or alternative editor. For more information, see System.ServiceModel Namespace on MSDN. For more information about using tracing with WCF, see Configuring Tracing on MSDN.

Although you can use any of the trace listeners supported by the Logging Application Block with WCF, the most common scenario is to log the messages in XML format. XML files logged from WCF can be analyzed in the Service Trace Viewer application that is included in the Windows SDK. To configure the Logging Application Block to log messages in this XML format, you should use the XmlTraceListener. This trace listener derives from the XmlWriterTraceListener, which is a part of the .NET Framework, and is able to extract the XML payload from an XmlLogEntry object and write this data to an XML text file. You can analyze the output of this trace listener with the WCF log file analysis tools.

A sample configuration file that demonstrates what the configuration should look like follows the next procedure. The <system.serviceModel> section of the file defines how the WCF service behaves and is not relevant to the actual logging process.

The following procedure describes how to integrate the Logging Application Block with applications that use WCF.

To configure the WCF-integration trace listeners

  1. Create or open a configuration file in one of the Enterprise Library configuration tools, and ensure the Logging Application Block is added to the application’s configuration. For more information, see Configuring Enterprise Library.
  2. Click the plus sign icon in the LoggingTarget Listeners pane, point to Add Logging Target Listener, and then click AddXML Trace Listener.
  3. (Optional) In the properties pane, set the Name, the File Name for the trace file, the Severity Filter for the level of message to detect, and select a value for the Trace Output Options property to specify which options or elements should be included in the trace output. For more information, see TraceOutputOptions Values.
  4. Click the plus sign icon in the Categories pane and click Add Category.
  5. In the properties pane, set the Name to System.ServiceModel; set Auto Flush and Minimum Severity as required.
  6. Click the Listeners property plus sign icon, then select the XML Trace Listener from the drop-down list.
  7. Save the configuration file.
  8. Open the configuration file either in Visual Studio® or in the text editor of your choice.
  9. Define the EntLib Proxy trace listener in the <system.diagnostics> section and use System.ServiceModel as the source. (See the sample configuration file.)
  10. Modify the WCF configuration to specify the desired level of logging, as shown in the following sample configuration file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration"
             type="Microsoft.Practices.EnterpriseLibrary
                  .Logging.Configuration.LoggingSettings,
                  Microsoft.Practices.EnterpriseLibrary.Logging" />
  </configSections>
  <loggingConfiguration name="Logging Application Block"
                        tracingEnabled="true"
                        defaultCategory="System.ServiceModel"
                        logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="c:\\trace-xml.log"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary
                            .Logging.Configuration.XmlTraceListenerData,
                            Microsoft.Practices.EnterpriseLibrary.Logging"
           traceOutputOptions="None"
           type="Microsoft.Practices.EnterpriseLibrary.Logging
                 .TraceListeners.XmlTraceListener,
                 Microsoft.Practices.EnterpriseLibrary.Logging"
           name="XML Trace Listener" />
    </listeners>
    <formatters>
    </formatters>
    <categorySources>
      <add switchValue="All" name="System.ServiceModel">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>
  <system.serviceModel>
     <diagnostics>
       <messageLogging logEntireMessage="true"
                       logMalformedMessages="true"
                       logMessagesAtTransportLevel="true" />
     </diagnostics>
    <services>
    <service name="WCFServiceLibrary1.service1"
             behaviorConfiguration="MyServiceTypeBehaviors" >
      <endpoint contract="WCFServiceLibrary1.IService1"
                binding="wsHttpBinding"/>
      <endpoint contract="IMetadataExchange" binding="mexHttpBinding"
                address="mex" />  
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MyServiceTypeBehaviors" >
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  </system.serviceModel>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="All">
        <listeners>
          <add name="traceListener" 
               type="Microsoft.Practices.EnterpriseLibrary.Logging
                     .TraceListeners.EntLibLoggingProxyTraceListener,
                     Microsoft.Practices.EnterpriseLibrary.Logging" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

For information about the WCF-integration trace listener properties, see Trace Listener Properties.