views:

26

answers:

1

We are using Enterprise Library for all our logging and exception handling needs in our app. We have added an email listener to send all the caught exceptions in email to the administrator. One requirement is that when an exception occurs in a method we need to retrieve the parameter values of the method if any and attach it to the exception details in the email sent. Is it possible without writing a custom logger?

A: 

Just create a custom exception, setting the message to the parameters:

try {
  ...
} catch(Exception ex) {
  var customException = new CustomException(ex, string.format("Param1 {0}, Param2 {1}", param1, param2));
  bool rethrow = ExceptionPolicy.HandleException(customException, PolicyName);
}

verified that in fact the ExceptionFormatter class will indeed traverse all inner exceptions, so your CustomException could be as simple as

public class CustomException : Exception
{
    public CustomException(string message, Exception innerException) : base(message, innerException)
    {
    }
}
Bryce Fischer
Thanks. Will try this. But will the existing exception details stay intact if we do this?
blntechie
If you write the CustomException so that it passes the "ex" to the base Exception class it should. Depends on if the Formatter outputs the InnerException details or not. I"m not sure about that last item... I haven't toyed around with teh Exception Block in some time...
Bryce Fischer
In fact, you could extend and write a new ExceptionFormatter http://msdn.microsoft.com/en-us/library/ff664587(v=PandP.50).aspx. Doesn't seem that involved actually... Then, your CustomException could load all the data you want and you can handle it in the ExceptionFOrmatter class.
Bryce Fischer
Sorry to spam contents, but looking at the ExceptionFormatter source, it does indeed output the InnerException if present.
Bryce Fischer
Thanks looking for the details, Bryce.
blntechie