Logging Application Blocks
Using the Enterprise Library Logging Application Block (LAB) makes it easier to implement common logging functions. The ADO.NET Entity Framework data provider uses the standard Logging Application Block and design patterns, and offer LAB customizations for additional functionality.
When Should You Use the LAB?
If your applications have a requirement to log information to a database, a message queue, the Windows Event Log, or Windows Management Instrumentation (WMI), the LAB provides this functionality. In particular, the LAB is useful if you need to filter logging messages based on category or priority, if you need to format the messages, or if you need to change the destination of the message without changing the application code.
Configuring the LAB
A logging capability can be added to an application by adding an entry to an applications 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 Psql_Enable_Logging_Application_Block_Trace to true. Alternatively, in the app.config file, set the AppSetting property Psql.EnableLoggingApplicationBlock to true.
A 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 must establish 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 must be the SQL that is ultimately transmitted to over the wire.
1
Select Start > Programs > Microsoft patterns and practices > Enterprise Library 4.1 > Enterprise Library Configuration. The Enterprise Library Configuration window appears.
2
Select File > New Application.
3
Right-click the Application Configuration node and select New > Logging Application Block.
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. The following example creates the category name PSQL Error.
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. In the Save As dialog, enter a name for your configuration file. By default, the file is saved to C:\Program Files\Microsoft Enterprise Library\Bin\filename.exe.config, where filename is the name that you entered for Save As.
Adding a New Logging Application Block Entry
Now, use the Enterprise Library Configuration Tool to add a new Logging Application Block entry:
1
Start 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.
3
In the TracingEnabled field, enter True.
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 use any version of the LAB other than the Microsoft Enterprise Library 4.1 binary release, you must set the LABAssemblyName. For example, if you use 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 a PSQL 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=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.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=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="Psql">
<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>