Where to log exceptions?

I have a layered web application, the layers are below.

  • MyApp.Models
  • MyApp.Views
  • MyApp.Controllers
  • MyApp.Services
  • MyApp.Repository.EF

In this architecture, I have a separate controller class project, as you might guess. Imagine I want to add a category to db in this application.

For this transaction, follow the steps below,

Controllers> Services> Repository> DB

For this transaction, I am following these method steps in the projects below,

CreateAction> CreateCategory> InsertEntity> CategoryTable

In all of these methods in the layers, I use try-catch blocks to log any exception. Am I really wondering if this is correct or not? I am very bored of writing try-catch blocks to log into the following steps for possible errors in each level.

What's the best practice for logging into a multi-tier application like mine?

+3


source to share


2 answers


Typically you add a global exception handler where you log the exception and convert it to a suitable format to send to the user (error page, WCF error, HTTP error, etc., depending on the technology you are using.

This means you don't need a try / catch attempt on your service, business, or other layer; unless you have specific logic to execute. Instead, you don't worry about constantly catching exceptions because they are handled by the global exception handler, so its internal details are not displayed, but are automatically logged.



An example of how to do this in ASP.NET MVC: take a look here .

+3


source


As a Java developer, I would create several new types of exceptions (Runtime, Checked, etc.) with a constructor taking other exception instances as parameters and / or other (business or technical) parameters. In this constructor, I can write what I want. This way, I will avoid all of those catch blocks made for logging.



+2


source







All Articles