Contextual Information in Exception Handling

Topics: Exception Handling Application Block
Aug 12, 2013 at 11:58 PM
What is the best practice for adding contextual information to an exception that will be wrapped by the Exception Handling Application Block?

The docs for EntLib 5.0 have this example:
public DataSet GetDataSet(string queryName)
{
  try
  {
    return RunQuery(queryName);
  }
  catch(Exception ex)
  { 
    string formattedInfo = FormatException(ex);
    Logging.Log(formattedInfo);
    throw new DataAccessException(
        "Database access failure for query" + queryName, e);
  }
}
As you can see, the contextual additional information queryName is appended to the exception message. How is this accomplished with EntLib? The documentation example seems to completely ignore queryName when using EntLib.

I've seen suggestions in the discussions (here) to use the exception's Data dictionary to include this information. However I see two issues with this:

1) If you use the original exception's data block the contextual information may not applicable (make sense) for the tier where the exception was thrown.

2) If you use the wrapped exception (out result of HandleException) you would not be honoring the the wrapping of the policy (i.e. you may include sensitive information that was intended to be shielded).

Another option might be to first wrap the caught exception as a higher level exception and then handle that. However this seems like a clear violation of the EngLib guidance.
public DataSet GetDataSet(string queryName)
{
  try
  {
    return RunQuery(queryName);
  }
  catch(Exception ex)
  { 
    var dataException = 
        new DataExecption("Database access failure for query" + queryName, ex);

    Exception exceptionToThrow; 
    if (exceptionManager.HandleException(
       dataException, "Exception Policy Name", out exceptionToThrow)) 
    { 
        if(exceptionToThrow == null) 
            throw; 
        else 
            throw exceptionToThrow; 
    }    
  }
}
It seems we need a way to pass a dictionary of data into HandleException and let the policy decide whether to show it or now.

How best to handle these situations?
Aug 13, 2013 at 1:44 AM
There is only one way (to my knowledge) to add additional context information to the exception for logging purposes and that is using the exception's Data IDictionary property. If you use the ExceptionManager.Process method you won't have the option to do that. Actually, Enterprise Library does not have support for arbitrary context information (besides adding ExtendedProperties and using the Tracer category).

You would either need to modify the source code to natively support the required functionality or perhaps create a custom LoggingExceptionHandler that will pull information from some stateful variable (e.g. a ThreadStatic variable).

Another approach might be to look at using Unity Interception (since all data is available) to log the required information when an exception occurs.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to