views:

305

answers:

4

If I have a multi-layer Winform app with a Presentation, Business and Data Layer for example, and I encounter an error in either the Business Layer or Data Layer for which the only logical action is to log the error and inform the user that an error has occurred where should the logging take place?

Should I put the the methods in the Business and Data Layers in try catch blocks like so

try
{
    DoSomethingThatMightGiveErrors();
}
catch(Exception ex)
{
    logger.log(ex.ToString());
    throw;
}

Or should I just let the errors bubble up to the presentation layer and handle the logging and informing the user there?

A: 

That depends on your requirements, like, for instance, do you want to put your users off your application as a result of errors showing up via bubbling up to the presentation layer? How often would these errors occur in unexpected situations?

This is a loaded question and every application is different, the most basic thing I can say is to use the try catch clauses in the business/data layers and ensure that you inform the users of certain situations where an error could be expected (You do have this in documentation?)

Other than that, check with the requirements and feedback from end users...if you allow the errors to appear on the presentation layer, the worst case is the user will refuse to work with it as a result of errors spewing out...

Hope this helps you, Best regards, Tom.

tommieb75
+1  A: 

If you are talking about unhandled (non-business related) exceptions just let them propagate to the UI layer where you could catch/log/inform the user.

Darin Dimitrov
This is what I would have said. But Gerrie's answer is a good one too
Seth Spearman
+3  A: 

I'd put the logging at the business layer level and then rethrow the error. If this layer is used in another project in the future, it's already doing the logging. Rethrowing the exception allows consumers of this layer to convert the error into a friendly message.

EDIT: I think it depends a bit on the kind of logging: if you're logging to a central database, which is independent from the UI, put the logging in the business logic layer. If the logging is specific to the UI, for example writing a log file to the application's directory, put it in the UI.

Gerrie Schenck
+1  A: 

My preference would be to place it in the business layer.

  1. If you ever change the nature of the presentation layer (i.e. winforms to webforms) the logging code won't need to be duplicated.
  2. All of your logging will be a lot easier to find and maintain, as you can always scan the business class's list of methods and inspect/tweak them for logging. If you put the logging in your presentation layer, the logging calls will be scattered all over the place - a single business class could have logging calls made in many different presentation classes.
Zack