views:

174

answers:

5

I've just started working on a brownfield ASP.NET 2.0 Web Application. As with a lot of brownfield applications, the architecture, and specifically the exception logging architecture, is inconsistent and in some places, missing or incorrect (swallowing exceptions, re-throwing, using them for control logic etc)

As a starting point for bringing all of this under control, I'd like to just do a blanket catch all of EVERY exception being thrown in the application, and log it away somewhere like a DB table. Unfortunately to explicity code that into the app would be many days and probably weeks of work, so that rules out hand coding things (eg. putting logger.log(ex) everywhere) and things like log4net and nlog. What I'm looking for another, more light-weight solution to the problem.

After some quick reading on here, I've come across various techniques like:

  • Aspect Oriented Programming, such as
    • Attribute-based
    • Post Sharp library
    • Microsoft Unity
    • Castle Unity
  • Web Handlers
    • ELMAH
    • Others?

What have people's experiences been with these solutions? Bearing in mind that I'd like to implement this as simply and painlessly as possible, so bells and whistles aren't required at this stage. All I need is something that will report every exception to a single location so I can start finding holes in the logic.

EDIT:

I should probably make it clear that I want to log **ALL** exceptions (even the ones that are handled, as some are handled incorrectly). Is it possible to configure Elmah to do this? If not, what are my other options?

+2  A: 

Don't add any catches. Enable ASP.NET Health Monitoring. If you like, you can configure it to log to a database.

Beyond that, I'd go with Enterprise Library, specifically the Exception Application Block and the Logging Application Block. Apply logging wherever you like (it can be configured off in production), but only catch exceptions at layer boundaries - like in your public DAL methods.


One of the benefits of Enterprise Library is that the Design for Operations project plays well with it, if this is the sort of thing you're interested in:

Project focused on developing tools and guidance to help enable the development of highly manageable applications on the Windows platform.

This project has created two deliverables. First is the Visual Studio Team System Management Model Designer Power Tool (TSMMD). TSMMD is a tool for modeling line-of-business health scenarios and the associated instrumentation. The tool includes guidance packages that generate platform instrumentation (called Instrumentation Helpers) and validators to confirm that application source code contains instrumentation defined in the Health Model. The tool can then be used to generate Management Packs for System Center Operations Manager 2007. Lastly is the Management Guide that contains prescriptive guidance on building highly manageable applications on the Microsoft Windows platform.

John Saunders
A: 

Consider using an Aspect Oriented Framework to inject logging code into your source pre-compile time. Logging is the textbook application of AOP, so most packages should be able to handle your requirements.

Here is a nice list of AOP libraries. http://www.bodden.de/tools/aop-dot-net/

I played around AOP in .NET about 4 years ago, looks like the libraries have matured significantly since then.

Ryan
+4  A: 

I thoroughly recommend elmah to plugin into your application. Its an eye opener to find out where things are going wrong. I've had several unreported time-outs in my application for example that I have identified and fixed.

As for simple and painless. It involves no changes to your code other than some extra lines to your web.config. You can add it and be viewing exceptions within 10 minutes.

John Nolan
John, I'm curious. Does elmah do anything that ASP.NET Health Monitoring doesn't do?
John Saunders
Hi John. Phil Haack compares the two http://haacked.com/archive/2007/07/24/securely-implement-elmah-for-plug-and-play-error-logging.aspx#46656 I'll leave it to him as I am no Health Monitor expert.
John Nolan
A: 

Does anyone have any ideas regarding my edit?

"I should probably make it clear that I want to log ALL exceptions (even the ones that are handled, as some are handled incorrectly). Is it possible to configure Elmah to do this? If not, what are my other options?"

jacko
A: 

If you want to stay away from touching existing code base, then, AOP ,as mentioned by others, is the way to go. I don't know how it works in .Net, but I personally try to stay away from this fancy technologies in Java because it loosens your control and has some performance penalty. Of course it depends on the nature of your system. As to the log management - try logFaces, it will give you an instant access to all your errors and save lots of hassle managing log files. Particular care is taken about exception stack traces and source code correlation.

Dima
How would it loosen your control? Wouldn't you have MORE control over your code base if you could inject aspects into the code? And I think the amount of exceptions being thrown at the moment has a far greater performance penalty than some additional logging.
jacko
In my modest experience, intercepting could be quite awkward particularly when you debug the things. AOP is really cool stuff, but I'd rather stick with conventional flow of the program when you know where things start and where they end. This is very subjective of course, but when I see that someone snatches the control of my calling sequence, I'd be nervous. There is definitely a performance penalty using dynamic proxies in Java, not sure about .Net. It sounds like you've got some problems in your existing code base and you're trying to fix all of it with one giant aspect-to-catch-all :)
Dima
Well, I'm definitely trying to LOG all exceptions with one giant aspect to catch all, however I get the feeling that to fix all of it is going to be a long long process... :)
jacko