In a layered web application I want to move all error logging from the Domain and Data layers to the global exception handler, but I'm not sure what is the trade-off. I want to remove any logging call and replace it by a more specific Exception (custom if it's necessary) or remove the catching:
try{
. . .
}
catch
{
Logger.Error('Info'); // <-- remove this for a: throw new CustomException('Info', ex);
throw; // <-- then, remove this line
}
There is a configured Global Exception Handler as middle-ware in the WebAPI, then as part of the handler method I'm going to log any exception occurred
// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler(
error =>
{
GlobalExceptionHandler.ErrorHandling(error, env);
});
}
// GlobalExceptionHandler.cs
public static class GlobalExceptionHandler
{
public static void ErrorHandling(IApplicationBuilder errorApp, IHostingEnvironment env)
{
errorApp.Run(async context =>
{
.
.
.
Log.Current.Error(exception.Message, () => exception);
}
}
}
Could be a better approach to avoid duplicated logging records?