Export (0) Print
Expand All

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.

Because WCF is only able to log directly to System.Diagnostics Trace Sources, it is necessary to configure a special trace listener called 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 carrying an XML payload.

The EntLibLoggingProxyTraceListener will add the name of its containing trace source as a category to each LogEntry it creates. Also, 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 categoriesXPathQueriesattribute 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, for 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 called “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. See System.ServiceModel Namespace on MSDN for more information. See Configuring Tracing on MSDN for more information on using tracing with WCF.

While 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 procedure. The relevant sections are in bold. 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 the Application Blocks.
  2. In the Trace Listeners node, right-click Trace Listeners, point to New, and then click XML Trace Listener.
  3. (Optional) Set the properties in the right pane.
  4. In the Category Sources node, right-click Category Sources, 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=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        name="XML Trace Listener" />
    </listeners>
    <formatters>
    </formatters>
    <categorySources>
      <addswitchValue="All" name="System.ServiceModel">
        <listeners>
          <addname="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>
       <messageLogginglogEntireMessage="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>
      <sourcename="System.ServiceModel" 
              switchValue="All"
              propagateActivity="true">
        <listeners>
          <addname="traceListener" 
              type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EntLibLoggingProxyTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" 
               />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

The next procedure describes how to configure the special trace sources. The LoggingErrors&Warnings special source receives log entries for errors and warnings that occur during the logging process. The UnprocessedCategory special source receives log entries for that are assigned to categories that are not processed by a category source. The AllEvents special source receives all log entries.

To configure the special trace sources

  1. In the SpecialSources node, click the special source you want to use.
  2. (Optional) Set the SourceLevel property. For information about the SwitchLevel property values, see SourceLevel Values.
  3. Repeat the procedure for each special source you add.

Ff650510.note(en-us,PandP.10).gifNote:
If an error occurs when the application block writes to a special trace source, there is no error reported and the original log message is discarded.

The next procedure shows how to assign log entries to categories.

To configure the category sources

  1. In the Category Sources node, right-click Category Sources, point to New, and then click CategorySource.
  2. (Optional) In the right pane, type the name of the category.
  3. Click the SwitchValue property and select the severity level of the events that will be assigned to this category. For information about the category source properties, see Category Source Properties. For information about the SourceValue property values, see SourceLevel Values.
  4. Repeat the procedure for each category you add.

You can filter log entries based on their categories and their priorities. You can also entirely enable or disable logging. The following procedure shows how to configure the filters.

To configure the filters

  1. In the Filter node, right-click Filters, point to New, and then click the filter you want to use.
  2. The CategoryFilter allows or denies log entries based on their categories:

    If you want to use a CategoryFilter, you can change the CategoryFilterExpression property by clicking the drop-down arrow. This opens the CategoryFilterEditor dialog box.

    Click Filter Mode. Next, add the categories that will be filtered. You can either select a name from the drop-down list or type a category name.

    Click Add Filter. The category name appears in the lower pane.

    To remove a category, click a category name in the lower pane, and then click Remove Filter. For information about the CategoryFilter properties, see Category Filter Properties.

  3. The LogEnabledFilter must be set to True to permit logging. The following procedure describes how to enable logging and how to change the name of the filter.

    If you want to log events, set the Enabled property to True.

    (Optional) Change the name of the LogEnabledFilter. For information about the LogEnabled Filter properties, see LogEnabled Filter Properties.

  4. The PriorityFilter allows or denies log entries based on their priorities, which are properties. The following procedure describes how to set the Priority Filter properties.

    (Optional) If you want to use a PriorityFilter, you can set the MaximumPriority property. This is the maximum priority value a log entry can have to be logged. If you do not set this property, the value is 2147483647 (this is the largest possible value of a 32-bit signed integer). For information about the PriorityFilter properties, see Priority Filter Properties.

    (Optional) You can set the MimimumPriority property. This is the minimum value a log entry must have to be logged. If you do not set this property, the value is -1.

    Type a new name for the name of the PriorityFilter.

  5. A Custom Filter is a class that you have created that derives from the LogFilter class. Its configuration information consists of a collection of name/value string pairs. For information about the CustomFilter properties, see Custom Filter Properties. The following procedure describes how to set the Custom Filter properties.

    If you are going to use a CustomFilter, type a new name for the filter.

    Set the Type property by clicking the filter type name in the TypeSelector dialog box. You must select a class that derives from the LogFilter class. It must also have CustomLogFilterData as the type specified as the value for the ConfigurationElementType attribute.

  6. Repeat the procedure for each filter you add.

The following procedure shows how to configure the Logging Application Block properties.

To configure the Logging Application Block

  1. In the LoggingApplicationBlock node, click each property you want to change . For information about the Logging Application Block properties, see Logging Application Block Properties.
  2. Set the properties if you need to. If you want to use a default category, click the drop-down arrow and select one of the category names. Log entries that are not assigned to a category belong to the default category.
  3. The LogWarningWhenNoCategoriesMatch property sends log entries that are assigned to a category that is not specified in configuration to the LoggingErrors&Warnings special source. If you want this to happen, click True in the drop-down list to enable this property.
  4. The TracingEnabled property specifies whether activity tracing is enabled. To enable it, click True in the drop-down list.

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.
Show:
© 2014 Microsoft