C#: Microsoft Enterprise Library: Logging
By: Zack Turnbow
Introduction
Continuing in the introduction series on the Microsoft Enterprise Library, this article will focus on the logging application block. One of the many issues concerning good software development practices is consistency, which is another advantage of using the Enterprise Library. The Logging Application Block (LAB) provides consistency along with the providing frameworks for auditing and tracing. It also offers the ability to create your own custom trace listeners in addition to configuring log options.
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
Implementation
Since this is a continuation, the project from the last article, Microsoft Enterprise Library: Data Access will be used as a starting point. For this article, the Enterprise Library Configuration application will be used. To install it, follow the steps here. So the next task to complete is to add the following reference to the Enterprise Logging dll.
[Click to see full-size]
Next, open up the Default.aspx C# code behind and add the following using statements:
using Microsoft.Practices.ObjectBuilder;
Once the above steps are complete, it’s time to write some code. In the page load event and after the GridView1 data bind method, code will be added to log the page load event. So go ahead and add the following code:
// Log the event
LogEntry log = new LogEntry();
log.EventId = 1;
log.Message = "Product page has been loaded";
log.Categories.Add("Trace");
Logger.Write(log);
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
The LogEntry class in the above code is used to create the actual log entry where as the Logger class actually does the act of logging the log message. The logging functionality needs to be configured next, so open up the Enterprise Library Configuration application.
[Click to see full-size]
Click the Open icon and navigate to where the web.config file is for this website.
[Click to see full-size]
When the file loads, it will look similar to this:
[Click to see full-size]
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
Next right click on the web.config in the left pane and select New – Logging Application Block. It will add more configuration parameters that need to be filled out. There will be 5 nodes under the LAB for filters, category sources, special sources, trace listeners, and formatters. Filters are where what needs to be logged based on the types category, priority, custom, or log enabled. The two sources are based on what needs to be logged. The trace listeners are where what is being logged gets sent to. Finally, formatters describe the format of the log. To continue, right click on the Trace Listeners node and select New – Flat File Trace Listener.
[Click to see full-size]
Over on the right pane, fill in the name of the listener, the full path of the filename of the log file, and the type of formatter to be used (this article will use Text Formatter).
[Click to see full-size]
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
Expand the Special Sources node, then right click the All Events node and select New – Trace Listener Reference. From there select the created flat file listener from above.
[Click to see full-size]
Save the configuration and exit the Enterprise Library Configuration application. Go back to Visual Studio and run the web site. When the grid has loaded, navigate to the location of the log file and view the logged event.
[Click to see full-size]
As shown, the Logging Application Block can save some development time when it comes to logging events in your application.
What have we learned?
How to use the Logging Application Block to log events in an application.
How to use the Enterprise Library Configuration application to add and configure the LAB.
Attachments
|