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.
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 the Open icon and navigate to where the web.config file is for this website.
When the file loads, it will look similar to this:
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.
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).
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.
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.
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










Leave a Comment
#1 Bob
Posted By: Bob | 12.08.2008 at 10:04 AM
Completely put off by the blatant advertising in the middle of the article. Sort it out.
#2 Vas
Posted By: Vas | 3.23.2009 at 12:28 PM
Very good step-by-step example, Thnaks
#3 Mayank Kukadia
Posted By: Mayank Kukadia | 7.10.2009 at 7:27 AM
Perfect soulution for flat text file logging.
Greate job.
Thank you.
#4 Abhinaw Kaushik
Posted By: Abhinaw Kaushik | 3.03.2010 at 1:14 AM
That's really a very good article.
Keep writing such kind of articles that will really help.
Thanks
Abhinaw
#5 http://www.top-online-casinos.de/
Posted By: http://www.top-online-casinos.de/ | 3.04.2010 at 11:35 PM
The Enterprise Library application blocks help address the common problems that developers face from one project to the next. They are designed to encapsulate the Microsoft recommended best practices for .NET applications..Different applications have different requirements, and you will not find that every application block is useful in every application that you build.....
#6 1080p trailers
Posted By: 1080p trailers | 3.07.2010 at 3:38 AM
Can you pls provide more information on this topic? Also your website is great. Take care!!
#7 220-702
Posted By: 220-702 | 3.19.2010 at 5:32 AM
For all I know this could be a really old entry, since there are no date marker on the article. In that case I apologize.
#8 Technology
Posted By: Technology | 3.25.2010 at 12:12 PM
Completely put off by the blatant advertising in the middle of the article. Sort it out.
#9 Alfred
Posted By: Alfred | 4.01.2010 at 10:14 AM
Today I used Cillit Bang to clean out my sink. It made me feel pleased for the rest of the day!
#10 Alfred
Posted By: Alfred | 4.01.2010 at 10:16 AM
But I liked the article as well.
#11 Timeshare Relief
Posted By: Timeshare Relief | 4.11.2010 at 9:56 PM
It helps a lot to me thanks for that, share also <a href="http://www.timesharerelief360.com/blog/">timeshare relief</a>.
#12 wholesale laptop battery
Posted By: wholesale laptop battery | 5.21.2010 at 1:13 AM
the project from the last article, Microsoft Enterprise Library: Data Access will be used as a starting point. For this article, the Enter
#13 wholesale shoes
Posted By: wholesale shoes | 5.24.2010 at 3:28 AM
Spring summer pocket perfect supporting role (figure) except heart shape brooch, earring, necklace,
#14 511 tactical
Posted By: 511 tactical | 6.01.2010 at 5:59 AM
Wish I have the luxury of time to consider using the benefits these site can offer.....
#15 Agile Informatics
Posted By: Agile Informatics | 6.10.2010 at 5:27 AM
This article is for those who have never encountered the Logging and Instrumentation Application Block, those who are looking to evaluate it, and those who have looked at it and thought it seemed like too much trouble. I will provide an overview of what features the Logging and Instrumentation Application Block provides, followed by a description of how to get the basics working in your environment.
#16 fast food restaurant
Posted By: fast food restaurant | 6.10.2010 at 6:10 AM
The Microsoft Enterprise Library was introduced in last week's column. It provides a collection of seven application blocks that utilize similar design patterns as well as development and configuration requirements.
#17 kuber infotek gupta
Posted By: kuber infotek gupta | 6.10.2010 at 6:41 AM
The Caching Application Block provides a flexible and extensible caching framework with set of API’s that can be used at any layers of an application. It support in-memory, database or isolated storage for storing the cached data. We can use it in web application as well as windows application.
#18 mbt shoes reviews
Posted By: mbt shoes reviews | 6.11.2010 at 3:49 AM
Yep, going to try to update
#19 coach outlet
Posted By: coach outlet | 6.12.2010 at 12:50 AM
Thank you for letting me know this information, I'll hold this information.
#20 affordable health insurance
Posted By: affordable health insurance | 6.12.2010 at 10:21 AM
Perfect soulution for flat text file logging.
#21 kuber infotek dinesh
Posted By: kuber infotek dinesh | 6.14.2010 at 5:18 AM
This article is a major re-working of my earlier Getting started with the Logging Application Block article. Whereas that article used Microsoft's Enterprise Instrumentation Framework and the stand-alone Logging Application Block, this article deals with the Logging and Instrumentation Application Block supplied as part of the Enterprise Library.
#22 michael jordan shoes
Posted By: michael jordan shoes | 6.18.2010 at 8:51 PM
good post!!thank you
#23 Bar Cocktail Shakers
Posted By: Bar Cocktail Shakers | 6.22.2010 at 2:55 AM
I had some similar question last year. In doing my occurrence, the most effective solutions are patient and confident. Be realistic bravely, and you will be alright now.
#24 tiffany jewelry
Posted By: tiffany jewelry | 7.18.2010 at 11:13 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#25 tiffany necklace
Posted By: tiffany necklace | 7.18.2010 at 11:14 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#26 cheap ugg boots
Posted By: cheap ugg boots | 7.18.2010 at 11:14 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#27 ugg short
Posted By: ugg short | 7.18.2010 at 11:14 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#28 ghd flat iron
Posted By: ghd flat iron | 7.18.2010 at 11:14 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it.
#29 ghd hair straightener
Posted By: ghd hair straightener | 7.18.2010 at 11:14 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#30 tiffany co
Posted By: tiffany co | 7.21.2010 at 2:22 AM
For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#31 tiffany jewelry
Posted By: tiffany jewelry | 7.21.2010 at 2:23 AM
For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#32 tiffany jewellery
Posted By: tiffany jewellery | 7.21.2010 at 2:23 AM
For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#33 congcong
Posted By: congcong | 7.21.2010 at 2:59 AM
<a href="http://www.imitatewatch.com/GoodsDetail/Replica-Hublot-Big-Bang-Mens-Watch-301-SB-131-RX-Watches-2213.html">Replica Hublot Big Bang Watches</a>
<a href="http://www.imitatewatch.com/GoodsSeries/Replica-Seamaster-Watches-296.html">Omega Seamaster Replica Watches</a>
<a href="http://www.imitatewatch.com/GoodsSeries/Replica-Luminor-Watches-311.html">Replica Panerai Luminor Watches</a>
<a href="http://www.imitatewatch.com/GoodsSeries/Replica-Daytona-Watches-363.html">Patek Philippe Celestial Watches</a>
<a href="http://www.imitatewatch.com/GoodsSeries/Replica-Daytona-Watches-363.html">Replica Rolex Daytona Watches</a>
<a href="http://www.imitatewatch.com/GoodsSeries/Replica-Pilot-Watches-248.html">Replica IWC Pilot Watches</a>
<a href="http://www.imitatewatch.com/GoodsSeries/Replica-Portuguese-Watches-250.html">Replica IWC Portuguese Watches</a>
<a href="http://www.imitatewatch.com/GoodsSeries/Replica-Royal_Oak_Offshore-Watches-29.html">Audemars Piguet Royal Oak Offshore Replica Watches</a>
<a href="http://www.imitatewatch.com/GoodsSeries/Replica-Happy_Sport-Watches-179.html">Chopard Happy Sport Replica Watches</a>
#34 congcong
Posted By: congcong | 7.21.2010 at 3:00 AM
<a href="http://www.luv-replica.com/GoodsBrand/Omega_Replica_Watches-16.html">Omega Replica Watches</a>
<a href="http://www.luv-replica.com/GoodsBrand/tag_heuer_replica_watches-20.html">Tag Heuer Replica Watches</a>
<a href="http://www.luv-replica.com/GoodsBrand/Paneri_Replica_watches-27.html">Panerai Replica Watches</a>
<a href="http://www.luv-replica.com/GoodsBrand/Patek_Philippe_Replica_Watches-17.html">Patek Philippe Replica Watches</a>
<a href="http://www.luv-replica.com/GoodsBrand/Swiss_Rolex_Replica_Watches-2.html">Swiss Rolex Replica Watches</a>
<a href="http://www.luv-replica.com/GoodsBrand/IWC_Replica_Watches-12.html">IWC Replica Watches</a>
<a href="http://www.luv-replica.com/GoodsBrand/Audemars_Piguet_Replica_Watches-3.html">Audemars Piguet Replica Watches</a>
<a href="http://www.luv-replica.com/GoodsBrand/Chopard_Replica_Watches-7.html">Chopard Replica Watches</a>
#35 official nike jordan shoes
Posted By: official nike jordan shoes | 7.25.2010 at 9:32 AM
now and the day after i will always concern you!
#36 tiffany ring
Posted By: tiffany ring | 7.26.2010 at 6:50 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#37 tiffany bracelet
Posted By: tiffany bracelet | 7.26.2010 at 6:50 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#38 tiffany necklace
Posted By: tiffany necklace | 7.26.2010 at 6:50 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#39 Gucci bags
Posted By: Gucci bags | 7.28.2010 at 10:32 PM
They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
#40 gucci bags
Posted By: gucci bags | 7.29.2010 at 10:25 PM
thank you