B Using Enterprise Library 4.1 : 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.
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
A logging capability can be added to an application by adding an entry to an application’s configuration file (either app.config or web.config) 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 the Logging Application Block output, set the environment property DDTek_Enable_Logging_Application_Block_Trace to true. Alternatively, in the app.config file, set the AppSetting property DDTek.EnableLoggingApplicationBlock to true.
The following C# code snippet shows the loggingConfiguration property of the app.config file.
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
Setting either of these properties to false disables the logging block.
If enabled, the data provider establishes a new LogEntry entry instance for each SQL statement generated by the ADO.NET Entity Framework canonical query tree.
The SQL logged to the Logging Block is the SQL that is ultimately transmitted to the data source.
To configure the Logging Application Block:
1
Select Start / Programs / Microsoft patterns and practices / Enterprise Library 4.1 - October 2008 / Enterprise Library Configuration. The Enterprise Library Configuration window appears.
Enterprise Library Configuration window
2
Select File / New Application.
3
Right-click the Application Configuration node and select New / Logging Application Block.
Logging Application Block is selected in the Enterprise Library Configuration window
4
Right-click Category Sources, and select New / Category.
5
In the Name pane, select Name. Type the name of the new category, and then press ENTER. In the following example, the category name DDTek Error will be created.
Enterprise Library Configuration window, with the Name field of the Category source selected.
6
7
Right-click the new category and select New / TraceListener Reference. A Formatted EventLog TraceListener node is added. From the ReferencedTraceListener drop-down list, select Formatted EventLog TraceListener.
8
9
Select File / Save Application. The Save As window appears. Type a name for your configuration file. By default, the file is saved to C:\Program Files\Microsoft Enterprise Library October 2008\Bin\filename.exe.config, where filename is the name that you typed in the Save As window.
Adding a New Logging Application Block Entry
Now, use the Enterprise Library Configuration Tool to add a new Logging Application Block entry:
1
Select Start / Programs / Microsoft patterns and practices / Enterprise Library 4.1 - October 2008 / Enterprise Library Configuration, and select File / New Application.
2
Right-click Application Configuration, then select New / Logging Application Block. The Configuration section appears in the right pane.
Adding a Logging Application Block in the Enterprise Library Configuration window
3
4
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.
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 4.1 (October 2008) 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.