Toolbox

Logging Web App Errors, Learning LINQ, and More

Scott Mitchell

Contents

Log and Review Web Application Errors
Blogs of Note
The Bookshelf

Log and Review Web Application Errors

In a perfect world there would be no errors in production. Database and Web servers would never go down, and code would be thoroughly tested and designed to gracefully handle any situation or input that might otherwise have caused an error. But we program in reality, where deadlines and never-ending feature requests are a constant and where database and Web servers can and do go offline. When errors occur in a production environment, it is essential that the details be logged and the development team promptly notified.

There are two common solutions for logging errors in an ASP.NET application: the ASP.NET health monitoring system and Atif Aziz's Error Logging Modules and Handlers (ELMAH). Both systems enable page developers to programmatically log the details of an error to a specified log source. Moreover, both offer mechanisms for automatically logging unhandled exceptions.

The health monitoring system from Microsoft is a robust, low-level framework that was introduced with ASP.NET 2.0. In addition to monitoring errors, it is capable of recording a variety of other metrics—including application lifecycle events such as application startup and shutdown, request-level events, and security-related events that include authorization violations and successful and failed login attempts.

When a monitored event occurs, it can be logged to any number of specified logging sources. The logging functionality in the health monitoring system is built atop the provider model, enabling developers to plug in a custom logging provider or to use any of the built-in providers. Among the built-in providers, there is one that records event details to the Windows® Event Log, another that writes the event to a SQL Server database, and one that sends the details to an e-mail address.

To begin using health monitor, simply add the appropriate configuration markup to the application's web.config file. The health monitoring configuration information must include the events to monitor, the logging providers, and how each monitored event is to be logged.

In addition to this basic information, the configuration markup can also include options such as whether the logging should be buffered, the minimum number of times an event must occur before logging commences, and the maximum number of times a given event should be recorded before future instances are ignored.

The other common logging option, ELMAH, is a set of open source HTTP handlers and modules. So while the ASP.NET health monitoring system can monitor a variety of events, ELMAH was designed to only log application errors. When an error occurs, ELMAH logs the details to a specified logging provider and, optionally, sends an e-mail to a configurable set of recipients.

There are a number of built-in and community-created ELMAH logging providers, including some that log to an XML file, to a SQL Server® database, to an Oracle database, and to the SQLite database (a lightweight, open source database engine).

ELMAH has some interesting display capabilities and supports all versions of ASP.NET. The ASP.NET health monitoring system does not provide any functionality for reviewing the logged events, but ELMAH includes built-in HTTP handlers that can display the error log from a Web page or as an RSS feed. And while health monitoring only works with ASP.NET 2.0 and beyond, ELMAH works with ASP.NET 1.0 and higher.

I first wrote about ELMAH in the August 2006 installment of Toolbox (see msdn.microsoft.com/msdnmag/issues/06/08/Toolbox). Since then, Atif has updated ELMAH to include a number of new features and performance enhancements such as exception filtering.

As I mentioned, by default, ELMAH logs all errors to the specified logging provider, however, now with exception filtering you can indicate under what circumstances an error should be dismissed (and therefore not logged). Furthermore, the latest version of ELMAH also gives you the ability to download the entire error log as a .csv file and provides improved performance for large error logs.

At their core, health monitoring and ELMAH provide the same functionality for logging errors in an ASP.NET application. Some developers prefer the health monitoring system because of its ability to monitor many different types of events. Others prefer ELMAH because of its built-in error log display capabilities and its support for ASP.NET version 1.x applications. In the long run, which error logging platform you choose is immaterial. What is essential, however, is that you have a rigorous error-logging system in place to record every single gory detail of those inevitable application errors.

Price: Free.

Health monitoring: go.microsoft.com/fwlink/?LinkId=109807

ELMAH: go.microsoft.com/fwlink/?LinkId=109809

Error Log in ELMAH

Error Log in ELMAH  (Click the image for a larger view)

Blogs of Note

I recently was talking with a colleague and mentioned a post from Jeff Atwood's blog, Coding Horror. My colleague had never heard of the blog and asked me what it was about. The best I could offer was that it covered "computers and programming and stuff."

It is difficult to describe Coding Horror, as it spans a wide and deep variety of topics. There are very technical posts (usually about ASP.NET and Web-related technologies) alongside suggestions for becoming a better programmer and making better project management decisions. Other posts evaluate computer hardware, comparing brands of motherboards, mice, or monitors. There are posts about usability and design flaws in consumer gadgets and clever fare such as dieting and exercise for geeks, the future of blogging, the problem with EULAs, and the history of DONKEY.BAS.

Jeff definitely cuts to the core of some important industry issues. Check out his views on the state of higher education for software students today. As Jeff points out, you can study the theory of Turing machines or you can learn where to click in Visual Studio®, or anything in between. So what's the best approach if you plan to build software? I'm not going to tell you. You are going to have to read Jeff's blog to find out.

The blog is popular—more than 75,000 people subscribe to its RSS feed—so there are tons of comments. Plus Jeff offers many links to other prominent bloggers so you really do get a good sampling of opinions out there. The only downside is the lack of tools for exploring this wealth of content. There is a search box but no breakdown of posts by category.

www.CodingHorror.com

Jeff Atwood's Blog, Coding Horror

Jeff Atwood's Blog, Coding Horror**  **(Click the image for a larger view)

The Bookshelf

One of the most important innovations in the Microsoft® .NET Framework 3.5 is LINQ. In short, LINQ enables developers to write C# and Visual Basic code that closely mirrors T-SQL's syntax, yet can be used to directly query any supported data source, including enumerable collections, XML documents, and SQL Server databases. For example, the following C# outputs the even numbers in the array defined here:

int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var evens = from p in numbers
            where p % 2 == 0
            select p;

foreach (var e in evens)
    Console.WriteLine(e);

Due to the new language syntax and the breadth of LINQ's features and complexity, I have preferred learning about LINQ through books rather than by taking the time to hunt and peck for online articles and tutorials. And the first LINQ book I picked up was Pro LINQ in C# 2008 by Joseph C. Rattz Jr.

This book starts with a quick and intriguing introduction to LINQ, illustrating how it can be used to query collections, XML files, and SQL Server databases. This is followed by a brief explanation of the new language features that made LINQ possible: lambda expressions, anonymous types, object initializers, extension methods, automatic properties, and so forth.

The bulk of the book is divided into four sections covering LINQ to Objects, LINQ to XML, LINQ to DataSets, and LINQ to SQL. Each section offers a thorough examination of the LINQ syntax and the various classes and methods that enable LINQ to query these different data stores. The LINQ to Objects section serves as a technical reference to the standard query operators. These operators are enumerated and are also accompanied by a short description and programming example. The subsequent sections, however, offer a richer discussion of LINQ, including helpful tips, common pitfalls, and more involved real-world examples.

The last section, LINQ to SQL, is probably the most interesting and useful chapter for the majority of developers. Not surprisingly, one-third of the volume is devoted to LINQ to SQL and includes step-by-step instructions for using the Object Relational Designer and the SQLMetal.exe command-line tool to create the underlying SQL entity classes. The author also presents examples for using these classes along with the DataContext class to query, update, insert, and delete data. What's more, there are also chapters that address more complex database issues, such as concurrency control and transactions.

Price: $44.99.

www.apress.com

  

Send your questions and comments for Scott to toolsmm@microsoft.com.

Scott Mitchell, author of numerous books and founder of 4GuysFromRolla.com, is an MVP who has been working with Microsoft Web technologies since 1998. Scott is an independent consultant, trainer, and writer. You can reach him via e-mail at Mitchell@4guysfromrolla.com or via his blog at ScottOnWriting.NET.