Enterprise Library 2.0: The Logging Application Block
The Enterprise Library 2.0 (EL2) is the second major release of the robust frameworks released by the Microsoft Patterns and Practices team. At the time this article was written, EL2 is a "Community Technology Preview (CTP)", so features are subject to change.
Major Changes in the Logging Block
Logging Block Definitions
- Trace Listeners: Collects, stores, and routes messages
- Database Trace Listener: Logs to a Database
- E-mail Trace Listener: Logs to an E-mail
- Flat File Trace Listener: Logs to Flat File
- Formatted Event Log Trace Listener: Logs to Windows event log
- Msmq Trace Listener: Logs to MSMQ
- System Diagnostic Trace Listener: Logs to an event to an implementation of System.Diagnostic
- WMI Trace Listener: Logs a Windows Management Instrumentation event
- Custom Trace Listener: Logs to custom implementation of trace listener
- Filters: Provides log filtering based on pre-defined criteria
- Category Sources: This is the grouping mechanism used to classify a type of a message. A category can consist of multiple Trace Listeners or a single Trace Listener.
- Special Sources: Out-of-the-box sources for use in your application. As far as I can tell, you cannot remove or add to these, you can only wire them up for use in your application by specifying a Trace Listener. These can be used as a generic or general way of logging information if you want to avoid the generation of categories.
- Formatters: Used to format a message
Jumping into Logging...
I assume you have the EL2 already. If not, you can obtain it from here. If you are using the December CTP, it is highly recommended that you build the EL2 configuration console prior to working with the examples in this article. The configuration console allows you to manipulate the configuration files through a simple GUI.
Along with the configuration console, the following examples require that you have Visual Studio 2005 and a MS SQL Server database installed. I used SQL 2005 Express, but I believe that, with some minor tweaks, any version should work.
EL2 Database Logging
To start the first example showing a simple database logging, navigate to the install directory of the Enterprise Library 2.0 and locate the "..\Src\Logging\TraceListeners\Database\Scripts\" directory. In this directory, you should find a batch file, "CreateLoggingDb.cmd," which will need to be executed (first ensure that SQL Server is running). This batch file executes the SQL script, which is located in the same directory and called LoggingDatabase.sql, creating the logging database natively used by EL2. The native logging database used in this example, "Logging," is very full-featured, but you do also have the choice to design and create your own database for use with the logging block.
Continue by creating a new C# Windows application in Visual Studio 2005; for ease of use, for this and all of the examples, Windows forms applications will be used. Add a new application configuration file, and open the Enterprise Library console.
EL2 is heavily driven through meta-data stored in application configuration files. The console creates a visual interpretation of the XML stored in these configuration files and makes it easy to manipulate the data. Navigate to File, Open Application, and select the app.config file that you just created. Another way to do this is to create the app config file in the EL2 console, but both methods have the same result.
The next step is to add a new node to the configuration by right-clicking (or navigating to "Action") in the console on the top-level node, and selecting "Logging Application Block."
Figure 1: The EL2 console is a straightforward GUI that reduces the time it takes to generate the required configuration information.
By default, the Logging application block gets configured with the EventLog trace listener enabled for the "General" category, and the "Special Source" category of "Logging Errors & Warnings." The event log is a fine logging store if you have a single server, but in most cases you want something more centralized that you can query against, such as a database.
For this example, you will need to remove all of the event log references by highlighting their nodes and going to "Action, Remove" or by right-clicking on their nodes and selecting "Remove." Add a new "Database Trace Listener" by highlighting the "Trace Listeners" node by right-clicking or going to "Action" again. You should see a new database trace listener and a new reference to the data access application block. The granular details about the data access block will be saved for another time, but do know that a new connection reference (which defaults to "Connection String") has been created. The setting attributes will need to be modified appropriately to reflect your database setup.
The next step is to associate the "Database Trace Listener" with the new database connection reference, and provide it a formatter; in this case, the default text formatter is used.
The final configuration step is to add a new category called "Audit" under "Category Sources" and reference the new "Database Trace Listener."
Rest assured, even though these steps are slightly time consuming, they are far simpler than editing the configuration files by hand. Downloading the example will help make sense of the process.
With the application configuration out of the way, the next step is very simple. Just add a reference in VS 2005 to Microsoft.Practices.EnterpriseLibrary.Logging, add the using statement (Using Microsoft.Practices.EnterpriseLibrary.Logging), and add the following code to a button:
LogEntry le = new LogEntry();
le.Categories.Add("Audit");
le.Message = "Testing our DB Logging";
le.EventId = 1234;
le.Title = "Database Message";
le.Priority = 1;
Logger.Write(le);
Downloads
Extending the Logging Application Block
EL2 and the Logging Application Block are highly extensible frameworks that have many predefined extension points. In this next example, you'll get a closer look at one of the extension points, the Custom Trace Listener, by creating a rolling flat file trace listener.
Note: The purpose of this next example is to demonstrate the extensibility of the logging application block. The example has not been tested for production use and probably is not the most efficient method for creating a rolling log file.
[ConfigurationElementType(typeof(CustomTraceListenerData))]
Figure 2: Use the EL2 console to configure name-value pairs for use by the Custom Trace Listener.
<listeners>
<add fileName="CustomLog{Date}.log" listenerDataType="..."
name="Custom TraceListener" initializeData="" />
...
</listener>
LogEntry le = new LogEntry();
le.Message = "Testing both our Custom Trace Listener and our
Database Trace Listener";
le.Categories.Add("Audit");
le.Categories.Add("Fatal");
le.EventId = 1234;
le.Title = "Two Categories";
le.Priority = 1;
Logger.Write(le);
The Easier, the Better
Related Links
- Enterprise Library Community: http://www.gotdotnet.com/codegallery/codegallery.aspx?id=295a464a-6072-4e25-94e2-91be63527327
- Patterns and Practices Home: http://msdn.microsoft.com/practices/
- Tom Hollander's Blog: http://blogs.msdn.com/tomholl/
No comments:
Post a Comment