Integrating the Logging Application Block with WCF Applications

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

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. Right-click the Trace Listeners node point to New, and then click XML Trace Listener.
  3. (Optional) In the properties pane, set the Name, the FileName for the trace file, and select a value for the TraceOutputOptions property to specify which options, or elements, should be included in the trace output.
  4. Right-click the Category Sources node, point to New and then click Category.
  5. In the properties pane, set the Name to System.ServiceModel.
  6. Right-click the System.ServiceModel node in the tree view, click New, and then click Trace Listener Reference.
  7. In the Properties pane, select ReferencedTraceListener, click the down-arrow button and select the XML Trace Listener.
  8. Save the configuration file.
  9. Open the configuration file either in Visual Studio or in the text editor of your choice.
  10. Define the EntLib Proxy Trace Listener in the <system.diagnostics> section and use System.ServiceModel as the source. (See the sample configuration file.)
  11. 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,
                                Version=4.1.0.0, Culture=neutral,
                                PublicKeyToken=31bf3856ad364e35"
               traceOutputOptions="None"
               type="Microsoft.Practices.EnterpriseLibrary.Logging
                     .TraceListeners.XmlTraceListener,
                     Microsoft.Practices.EnterpriseLibrary.Logging,
                     Version=4.1.0.0, Culture=neutral,
                     PublicKeyToken=31bf3856ad364e35"
       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 Logging Application Block Properties.


Show: