Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception.Data info is missing in EntLib log

How do I get the Exception Handling Application Block (EHAB) to write the values from the Exception.Data property in the log?

  try
  {
    // ...
  }
  catch (Exception ex)
  {
    ex.Data.Add("Hello", "World");
    throw ex;
  }

The exception is logged correctly, but I can’t find the added data anywhere in the log entry created by EHAB.

As far as I understand, it’s a recommended practice to add additional relevant information to the exception itself like in the above example. That’s why I’m a little surprised that EHAB doesn’t include this by default.

Can I fix this by modifying the template with the EntLib Text Formatter Template Editor (screen shot below)? I can't find any info on the various "tokens" provided, but I assume the answer is hidden somewhere with them.

Text Formatter Template Editor http://img195.imageshack.us/img195/6614/capturegmg.png

Or do I really need to implement my own custom Text Formatter to accomplish this?

EDIT/UPDATE:

I do this in my Global.asax.cs in order to avoid having to add the HandleException method call everywhere in my code:

using EntLib = Microsoft.Practices.EnterpriseLibrary;
using System;

namespace MyApp
{
  public class Global : System.Web.HttpApplication
  {
    protected void Application_Error(object sender, EventArgs e)
    {
      // I have an "All Exceptions" policy in place...
      EntLib.ExceptionHandling.ExceptionPolicy.HandleException(Server.GetLastError(), "All Exceptions");
      // I believe it's the GetLastError that's somehow returning a "lessor" exception
    }
  }
}

It turns out this is not the same as this (which works fine, and essentially solves my problem):

try
{
  // ...
}
catch (Exception ex)
{
  ex.Data.Add("Hello", "World");
  bool rethrow = ExceptionPolicy.HandleException(ex, "Log Only Policy");
  throw ex;
}

Going through all the code and adding try-catch's with a HandleException call just seems ... well, stupid. I guess my problem really is how to use EHAB correctly, not configuration.

Any suggestion on how I can properly log all exceptions on a "global level" in an ASP.NET web application??

like image 661
Jakob Gade Avatar asked Aug 18 '09 03:08

Jakob Gade


1 Answers

You shouldn't have to create your own formatter.

Each item in the Data IDictionary on the Exception object is added to the ExtendedProperties Dictionary of the LogEntry object and is logged (if specified by the formatter).

The Extended Properties:{dictionary({key} - {value})} config snippet should log all key/value pairs in the extended properties dictionary. If you want to log just one item from the collection you can use "keyvalue". In your example it would be something along the lines of:

Hello Key: {keyvalue(Hello)}


I modified the ExceptionHandlingWithLoggingQuickStart to add Data.Add("Hello", "World") and I see "Extended Properties: Hello - World" at the end of the Event Log entry. So it is working.

If you aren't seeing that behavior, you need to ensure:

  • That your exception with the Data items added is passed in to ExceptionPolicy.HandleException
  • That the policy specified in the HandleException method is configured for logging with a LoggingExceptionHandler in the configuration
  • That the formatter that is specified in the LoggingConfiguration is configured to support ExtendedProperties


If you can't see what the problem is try comparing what you have with what is in the QuickStart. If it's still not working, post your code and your config.

UPDATE:

If you are going to handle Application_Error in the global.asax then you will be receiving an HttpUnhandledException since the page did not handle the error. You can retrieve the Exception you are interested in by using the InnerException Property; the InnerException will contain the original Exception (unless it is in that exception's InnerException :) ). Alternately, you can use Server.GetLastError().GetBaseException() to retrieve the exception which is the root cause.

like image 78
Randy supports Monica Avatar answered Sep 23 '22 02:09

Randy supports Monica