1 Diagnostic Tools : Using the Logging Application Block

Using the Logging Application Block
Applications that use the standard Logging Application Block (LAB) and Microsoft patterns and practices can quickly replay the SQL generated when using the DataDirect data providers for ADO.NET Entity Framework. The SQL logged to the Logging Block is the SQL that is ultimately transmitted to the application.
Recording the SQL emitted by the ADO.NET Entity Framework is useful for database administrators. In the current release, LAB support can be used for general logging of Errors, DbCommandTrees, SQL, and Parameters.
To use features of the Enterprise Library with your data provider, download Microsoft Enterprise Library 5.0 (April 2010) from http://www.codeplex.com/entlib. The installation by default includes the documentation for the Enterprise Library 5.0. This documentation includes extensive information on using the Logging Application Block.
NOTE: Enterprise Library 5.0 requires Windows 8, or Windows 7, or Windows Vista SP2, or Windows Server 2012, or Windows Server 2003 SP2. If you are using the data providers on Windows XP, you can use Enterprise Library 4.1 (October 2008). Refer to the DataDirect Connect Series for ADO.NET Reference for more information about configuration changes needed to use Enterprise Library 4.1.
Configuring the Logging Application Block
To enable the DataDirect Connect logging features, you must configure the Logging Application Block. Refer to “Logging Application Blocks” in Chapter 4 of the DataDirect Connect Series for ADO.NET Reference for more information.
Before you add the Logging Application block to your application, open the Enterprise Library Configuration tool and ensure that the following categories are defined:
Using the Logging Application Block in Application Code
The LAB that you configured must be added to the app.config or web.config file for your application.
The following settings can be used to enable and configure the data provider's interaction with the LAB.
NOTE: If you are using any version of the LAB other than the Microsoft Enterprise Library 5.0 (April 2010) binary release, you must set the LABAssemblyName. For example, if you are using an older or newer version of the LAB, or a version that you have customized, you must specify a value for LABAssemblyName.
The following code fragment provides an example of a Logging Application Block that could be added to an Oracle data access application.
<loggingConfiguration name="Logging Application Block"
   tracingEnabled="true"
   defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
   <listeners>
      <add fileName="rolling.log"
         footer="----------------------------------------"
         header="----------------------------------------"
         rollFileExistsBehavior="Overwrite"
         rollInterval="None" rollSizeKB="0"
         timeStampPattern="yyyy-MM-dd"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               name="Rolling Flat File Trace Listener" />
      </listeners>
      <formatters>
         <add template="Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;&#xD;&#xA;"
               type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               name="Text Formatter" />
      </formatters>
      <categorySources>
            <add switchValue="All" name="DDTek">
               <listeners>
                  <add name="Rolling Flat File 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">
               <listeners>
                  <add name="Rolling Flat File Trace Listener" />
               </listeners>
            </errors>
          </specialSources>
   </loggingConfiguration>
Establishing a Listener for the Log
The Logging Application Block includes some trace listeners. In some cases, these listeners will not satisfy your application's requirements. In this case, you must create your own trace listeners.
Create a new class for the trace listener, and specify that it inherits from the CustomTraceListener class. Override the TraceData method to format the LogEntry object and write the information to the output destination.
After you configure the application to use the Logging Application Block, use the Enterprise Library configuration tools to create a new custom trace listener and specify your trace listener's type name and any other custom configuration properties. Create or modify existing categories to use the trace listener as a destination.
Refer to “Logging Application Blocks” in Chapter 4 of the DataDirect Connect Series for ADO.NET Reference for information on configuring the application, and to the Microsoft Enterprise Library 5.0 documentation for additional information on using Logging Application Blocks.
When to Use the Logging Application Block
The DataDirect ADO.NET Entity Framework data providers include a set of LAB customizations that are useful for developing with the ADO.NET Entity Framework when you want to log the Command Trees and SQL generated when using the data provider.