Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Take Advantage of the Logging Block in Enterprise Library 3.0 : Page 2

Adding reusable, configurable, and robust logging to your applications can't get much easier.


advertisement
Using the Enterprise Library Configuration Tool
Although you can manually configure the logging entries in the app.config file as discussed in the preceding section, you'll be glad to know that it's far easier if you use the Configuration Tool that ships with Enterprise Library. To do that, open the Enterprise Library Configuration tool, create a new application, and then right-click on Application Configuration and select New → Logging Application Block (see Figure 3).

 
Figure 3. Enterprise Library Configuration Tool: This tool lets you specify the logging configuration settings using a wizard.
The logging block configuration schema consists of four key elements named Category Sources, Special Sources, Trace Listeners, and Formatters. By default, the logging application block creates a Formatted EventLog TraceListener entry and configures it as the category source with the Text Formatter as the default formatter. Here's how to set up a new XML log through the Configuration Tool. Right-click the Trace Listeners node and select New → XML Trace Listener from the context menu. After creating the new listener, change the file name property on the right-hand pane to C:\Temp\Trace.xml.

The next step is to reference the new listener from the event sources. Right-click the All Events node and select New → Trace Listener Reference from the context menu. After creating the trace listener reference, set the ReferencedTraceListener property on the right pane to XML Trace Listener. When you've finished the changes, the wizard should show output similar to Figure 4.

 
Figure 4. Route Log to XML Trace Listener: By referencing the XML Trace Listener from the "All Events" node, you can route the logging requests for all events to the XML Trace Listener.
 
Figure 5. Logged XML Output: Here's the type of XML output produced by logging the message in an XML file. Note that the two main elements in each logged event (System and ApplicationData) contain event and system specific attributes.
Save the configuration settings from the Configuration Tool as app.config. If you now include the generated app.config file in your existing C# project example, you'll see XML generated in the log file (see Figure 5).


That's all you have to do to enable simple logging scenarios using the Configuration Tool. Using a Database as the Log Store
You've explored the steps involved in writing log information to an event log and an XML file. Although this approach works for most simple scenarios, for more complex applications, you might want to write the log information to a persistent data store such as a SQL Server database. Logging to a database makes it easy to write queries against the logged messages for troubleshooting purposes. The logging block comes bundled with an out-of-the-box database trace listener that is capable of writing to a data store.

To use the database-driven logging, you first need to execute a SQL script (supplied with Enterprise Library) to create the logging database. Run the LoggingDatabase.sql file that you'll find in the <Drive Name>\EntLib3Src\App Blocks\Src\Logging\TraceListeners\Database\Scripts folder. This will create a database named Logging in your SQL Server along with the required tables and stored procedures. Now, you need to modify your app.config file to use the "Database Trace Listener." You will also need to specify the database connection string. Listing 2 shows a complete example configuration file.

Author's Note: Make sure to edit the connection string in the <connectionStrings> element in Listing 2 to match your SQL Server before using the app.config file.

Next, open the Windows Forms application created in the previous section and simply switch the existing app.config file with the app.config you just created. Run the sample application again. This time, after clicking the button to execute the logging code, if you open the Log table in the Logging database, you will see the created log entry. Implementing Custom Trace Listeners
If the built-in trace listeners prove insufficient to meet your needs, the logging block lets you implement custom trace listeners by deriving from the CustomTraceListener class. That customization is beyond the scope of this article, but you can find more information in this MSDN article.

You've seen how adding the logging block lets you create flexible and easy-to-maintain logging code in .NET applications, using configuration and straightforward code to log messages to files, event logs, and SQL Server. Sure, the configuration's a little verbose, but that sure beats writing repetitive code, or worse, changing your code just to switch log stores. And because the Enterprise Library Configuration Tool makes the configuration a wizard-driven instead of a memorize-and-pray procedure, you should now have little trouble adding robust logging to all your applications.



Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap