Adding action to execute on Exception before Handling error

Topics: Exception Handling Application Block, General discussion, Semantic Logging Application Block
Sep 18, 2014 at 2:29 PM
Edited Sep 18, 2014 at 2:31 PM
Hello, I was trying to use ExceptionManager in combination with SemanticLogging, using the Process function. Actually I want to log the error if an exception occurred before handling the error. Right now the possibility that I know I have is to create a custom IExceptionHandler but the problem is that I can not use declarative or fluent configuration. So I was thinking about adding a action(function would also complete the request) to execute if an error occurred, as a last parameter to the existing Process function like this:
 public void Process(Action action, string policyName, Action<Exception> exceptionFunc)
 {
        if (action == null)
            throw new ArgumentNullException("action");
        if (policyName == null)
            throw new ArgumentNullException("policyName");
        try
        {
            action();
        }
        catch (Exception ex)
        {
            exceptionFunc(ex);
            if (!this.HandleException(ex, policyName))
                return;
            throw;
        }
  }

and call it like this:
_exManager.Process(() =>
{
     throw new Exception("about exception message");
}, POLICY, e => KtsEventSource.Log.AboutFailure(e.Message, e.GetType().FullName));
Right now I am using a custom class to inherit ExceptionManager so that I can use the Process function.
What do you think about this approach? Wouldn't it be nice to implement it?
Sep 18, 2014 at 6:25 PM
Take a look at Exception handling block with semantic logging. That answer is very similar to your approach. The main difference is that the Action<> is different because an EHAB formatter is used to format the exception. The block is configured using straight programmatic configuration without using fluent or declarative configuration. This should work without referencing System.Configuration (I assume that is your limitation?).


~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Sep 18, 2014 at 8:39 PM
Thanx for your answer and on the other side sorry, I think I might have misspoke regarding to declarative or fluent configuration. What I meant is that using custom IExceptionHandler I can not use declarative or fluent configuration and the other unusual part in the example you supplied is that somehow it leads to general purpose logging. Practically, wouldn't we tie together the event logging with the Policy too much? I mean , what if I want to use the same policy but use different logging on exception of type Exception? And what if I want to add additional contextual data to the log, can I add other than exception?
I am just trying to understand it right...;)
Sep 19, 2014 at 1:30 PM
There is no reason that you can't use a custom IExceptionHandler with declarative or fluent configuration -- you will just have to write the configuration code.

As to the question of the coupling the logging with the policy that approach is built into the the design of the block. Policies are static/defined at config time and each policy handles one or more types of exceptions and executes HandleException (public Exception HandleException(Exception exception, Guid handlingInstanceId)). If you look at the Logging Exception Handler the config values (e.g. category, event id, severity, etc.) are all static as well.

In terms of SLAB you can extend the SLAB LoggingExceptionHandler sample to support any EventSource with a valid signature.

Some approaches you can use are to create separate policies for each scenario or programmatically create separate ExceptionManagers for each scenario or go with the approach you show above.

If you are writing an enhanced Process method that takes an Action<Exception> it makes me wonder if you need the EHAB at all. If you are just logging then it seems like overkill but if you have an existing investment or you want to use some of the features (e.g. exception shielding/mapping) then it might be worth creating a custom ExceptionManager.

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