0

I am using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling for handling exceptions in the application.

I have a handler that logs the exception in our custom logging class. This handler is a part of exception policy definition that has NotifyRethrow as PostHandlingAction.

This is the handler that logs the exception

[ConfigurationElementType(typeof(CustomHandlerData))]
public class ExceptionLoggingHandler : IExceptionHandler
{
    public Exception HandleException(Exception exception, Guid handlingInstanceId)
    {
        Log.Write(exception);
        return exception;
    }
}

This is my exception policy definition

var throwAndLogPolicy = new ExceptionPolicyDefinition(PolicyType.LogAndThrow, new List<ExceptionPolicyEntry>()
{
    new ExceptionPolicyEntry(
        typeof(Exception),
        PostHandlingAction.NotifyRethrow,
        new IExceptionHandler[]
        {
            new ExceptionLoggingHandler()
        })                
});

I want to avoid logging the same excpetion twice with this handler. Is this possible with ExceptionHandling application block.

Say we have a MethodA that calls a MethodB that in turn throws an excpetion.

public void MethodA()
{
    try
    {
        MethodB();
    }
    catch(Exception ex)
    {
        if(ExceptionPolicy.HandleException(ex, "LogAndRethrow"))
            throw;
    }
}

public void MethodB()
{
    try
    {
        CallCodeThatThrowsAnException();
    }
    catch(Exception ex)
    {
        if(ExceptionPolicy.HandleException(ex, "LogAndRethrow"))
            throw;
    }
}

This example is legit. MethodB can be called from some other context rather than from MethodA. It just so happens that in this case it is called from MethodA and that method itself has an exception handling block. As you can see this would log my exception twice.

The only solution I came up with is to have a collection of thrown exceptions inside the ExceptionLoggingHandler to keep track of logged exceptions. This way I would log one exception only once. The problem with this solution is that I do not know when to remove the excpetion.

Robert
  • 2,407
  • 1
  • 24
  • 35
  • Why are you re-throwing once it's caught? Where does the exception ultimately get dealt with? Or do you just allow your application to crash with an unhandled exception? – rory.ap May 08 '17 at 11:23
  • At this point the exception is just logged and rethrown but somewhere up the call stack it will be caught and shown. I thought of maybe dealing with it there. Logging I mean. But, this is just not doable since the project that holds the business logic is separate than the one holding the UI. Effectivly UI would deal with the exception. The problem with this is that I might use Business Logic project somewhere else and I would still need it to log and rethrow. – Robert May 08 '17 at 11:26
  • The exception should be dealt with wherever it makes sense to do so. That might be right in the method where it's thrown, or it might be up the call stack a bit if it makes sense on that layer. Or it could go all the way up to the UI and be presented to the user in an alert or message. I think exception logging should only be applied in cases where it makes sense to log an exception. For instance, if you use an IO exception to determine that a file is locked for reading and inform the user of that, then should that really be logged? It's just a mechanism for getting info about a file. – rory.ap May 08 '17 at 11:41
  • I want to log any exception that happens in the system. This is so we can take the logs from our clients and examine them on our side. I think programmer shouldn't worry about wether HE should log it or not. He should just worry about applying the correct exception policy. That exception policy in turn will determine if the exception is handled, thrown again, shown, replaced or something completely different. – Robert May 08 '17 at 11:51

0 Answers0