4 Using the Microsoft Enterprise Library : Logging Application Blocks

Logging Application Blocks
Using the Enterprise Library Logging Application Block (LAB) makes it easier to implement common logging functions. DataDirect Connect data providers that support the ADO.NET Entity Framework use the standard Logging Application Block and design patterns, and offer LAB customizations for additional functionality.
To use features of the Enterprise Library with your data provider, download Microsoft Enterprise Library from http://www.codeplex.com/entlib. The Enterprise Library installation by default includes the Enterprise Library documentation, which contains detailed information about using the application blocks.
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).
When Should You Use the LAB?
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.
Configuring the LAB
Logging capability can be added to an application by adding an entry to an application’s app.config or web.config configuration file using the Enterprise Library configuration tool. This tool contains specific instructions in order to enable the Logging Application Block config file. The tool also contains the necessary AppSetting to enable the LAB.
To enable Logging Application Block output, set the environment property DDTek_Enable_Logging_Application_Block_Trace to true. Alternatively, in the app.config file, set the EnableLoggingApplicationBlock AppSetting property to true.
To disables the Logging Application Block, set either of these properties to false.
The following configuration XML snippet from the app.config file enables logging for the Oracle Entity Framework data provider.
<configuration>
<configSections>
<section name="ddtek.oracle.entity" type="DDTek.Oracle.Entity.OracleEntitySettings, DDTek.Oracle.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=c84cd5c63851e072" />
</configSections>
<ddtek.oracle.entity
    EnableLoggingApplicationBlock="true"
/>
The SQL logged to the Logging Block is the SQL that is ultimately transmitted to the data source.
The following procedure uses the configuration options for the .NET Framework 3.5.
To configure the Logging Application Block on any supported platform:
1
Select Start / Programs / Microsoft patterns and practices / Enterprise Library 5.0 / Enterprise Library Configuration / EntLib Config .NET 3.5. The Enterprise Library Configuration window appears.
Enterprise Library Configuration window
2
Select Blocks / Add Logging Settings. Additional fields appear on the New Configuration window.
New Configuration window with Logging Settings options displayed
3
a
Click the Logging Target Listeners plus sign button (Plus sign button). Then, select Add Logging Target Listeners / Add Flat File Trace Listener.
b
c
4
The Categories section in the Enterprise Library Configuration window is expanded. The Category subsection shows the default settings.
5
a
b
c
6
Repeat Step 3 through Step 5 to create the following categories:
The Enterprise Library Configuration window shows the configuration for a file named labconfig.config.
7
Select File / Save As. The Save Configuration File window appears. Type a name for your configuration file. By default, the file is saved to C:\Program Files\Microsoft Enterprise Library 5.0\Bin\filename.exe.config, where filename is the name that you typed in the Save Configuration File window.
Using the LAB in Application Code
The LAB that you configured must be added to the app.config or web.config file for your application.
Table 4-1 describes settings you can use to enable and configure the data provider's interaction with the LAB.
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>
Using Different Versions of the Logging Application Block
By default, the Entity Framework data providers use the Enterprise Library 5.0 Logging Application Block. If you need to use a different version of the Logging Application Block, you can specify the labAssemblyName setting in your .config file.
See “Specifying Enterprise Library Version Information in the .config File” for more information.