Reputation: 7669
I have only register database logger like this:
loggerFactory.AddDatabaseLogger(logRepository,serviceProvider,LogLevel.Error);
And after that all my unhandled errors are logged into database. When I add my custom error filter all unhandled errors are logged twice.
services.AddMvc(config =>
{
config.Filters.Add(new CustomExceptionFilter(_loggerFactory));
});
Is creating custom global exception filter unnecessary with latest version of ASP.NET 5 RC2?
Upvotes: 4
Views: 3136
Reputation: 57959
Exception filters are MVC concept and since unhandled exceptions can occur even outside MVC, only using ExceptionFilters might not suffice.
If you want to handle all unhandled exceptions in one central place, I think you could use the ExceptionHandlerMiddleware
. This middleware has the option of executing a controller's action. Example: app.UseExceptionHandler(errorHandlingPath: "/Home/Error");
In the Error
action you could log the exception details. You could retrieve the exception details by doing the following:
var exceptionHandlerFeature = HttpContext.Features.Get<IExceptionHandlerFeature>();
var exception = exceptionHandlerFeature.Error;
On a side note, if you are writing exception filters, you should do the following in the exception filter to stop propagating the exception again. Probably this is the reason you are seeing the log entries twice.
exceptionContext.ExceptionHandled = true;
Upvotes: 4