Logging HttpRequest Data in ASP.NET 2.0 Exceptions

Topics: Exception Handling Application Block
Mar 28, 2007 at 2:48 PM
Our Ops group requested that we add some data to our exception logs. Everything they are looking for can easily be obtained from the HttpRequest object, but I can't tell the best way to get that information into the Exception so it can be handled and logged by the EHAB. I've seen approaches from writing custom ExceptionHandler and ExceptionFormatter objects, to just putting the data into the Exception.Data property.

I've got a pretty short turn around time for this. Any suggestions on best practices or preferred approach?

Thanks,

Nate Southerland, MCSD.NET
Mar 28, 2007 at 3:22 PM
You will be happy that it is much simpler than that. You can add the information you need to the exception class like:

            try
            {
                Int32.Parse("A");
            }
            catch(Exception ex)
            {
                // Add Data That Will Get in Log
                ex.Data.Add("MyKey","MyData");
 
 
                bool rethrow = ExceptionPolicy.HandleException(ex, "LoggingPolicy");
                if (rethrow)
                    throw;
            }

By adding the data to the exception as shown above, the same information will get into the log.

Conceptually, the logginghandler copies the exception data to the LogEntry.ExtendedProperties to be logged by the TraceListener(s).

Just make sure your Formatter Template contains the appropriate tokens to display the ExtendedProperties in the log.

Regards,

Dave

__________________________

David Hayden
Microsoft MVP C#