|
Automate Exception Logging
Automate exception logging with one line of client code, control it through an App.config file without recompiling, and use custom publishers to craft cool logging tools.
by Craig Utley
Posted April 9, 2004
Technology Toolbox: C#, SQL Server 2000, XML
You probably perform the same development tasks repeatedly—some simple, some complex. Microsoft is creating a series of components called application blocks to help you implement best practices based on Microsoft's evaluation of a number of production applications. These best practices are often based on design patterns, something the Java world has been doing for quite a while.
Application blocks represent an interesting departure from the way Microsoft has distributed tools in the past. They come as Visual Studio .NET projects in both C# and VB.NET, so you get the full source code. They also come uncompiled, so you must open the project and compile it before you use them. Lastly, they tend to include thorough documentation.
A good example of a repetitive developer task is logging exceptions. You must catch exceptions when they're thrown, because you want your application to recover gracefully. However, you should log these exceptions also so you can examine them later and fix whatever is causing them.
Microsoft built the Exception Management Application Block (EMAB) to help you log exceptions with a single line of code. You can turn logging on or off in the config file, so you don't have to recompile your application. You can modify parameters of the logging in the config file, and you can write your own publishers that let you publish anywhere, including in text files, e-mail, and databases.
You can download EMAB from Microsoft (see Additional Resources for details). Install ExceptionManagementApplicationBlock.msi, creating a program group you can use to open the projects to compile them. Click on Start | All Programs, and you'll see Microsoft Application Blocks for .NET. Expand that and you'll see Exception Management, then a link to the source code in C# and VB.NET, plus a link for the documentation. Expanding the Source Code node for one of the languages provides a direct link to the Exception Management Application Block project. The project loads in Visual Studio .NET when you open this. You must convert the project if you have VS.NET 2003, but it's a simple process.
Open EMAB's source code, which contains two projects: an Exception Management class library and an Exception Management Interfaces class library. Compile both projects and begin using EMAB in your applications. Make sure you have administrative privileges on your computer so you can build and modify the project. If you don't, you need to change the security permissions in the directory in which you installed EMAB.
Back to top
|