4

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?

3
  • what package contains definition of AddDatabaseLogger method? Commented Jun 17, 2016 at 13:58
  • @Set I created custom implementation of EntityFramework logger there. Something similar like in this project. github.com/staff0rd/entityframework-logging/tree/master/src/… What I do not understand is how unhandled exceptions are automatically logged by ASP.NET 5 rc2 Commented Jun 17, 2016 at 15:52
  • 1
    If you use ASP Core Diagnostic package, this is implemented by ExceptionHandlerMiddleware github.com/aspnet/Diagnostics/blob/dev/src/…. In general, Middleware in ASP.Core is a component, that is assembled into an application pipeline to handle (all/any) requests and responses Commented Jun 17, 2016 at 18:18

1 Answer 1

4

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;

Sign up to request clarification or add additional context in comments.

3 Comments

I would test this exceptionContext.ExceptionHandled = true on Monday and let you know. I am not sure about middleware. I have seen this but not fully understand what is the benefit using middleware for logging exceptions. What kind of exceptions I can catch with middleware and not inside ASP.NET MVC errorFilter? Thanks for your help.
New version ASP.NET Core RC2 does not have exceptionContext.ExceptionHandled = true or anything similar. Not sure how to solve this issue.
To make error not be propagated you must use context.Exception = null; However then you need to form error result page like this: MediaTypeCollection mediaTypeCollection = new MediaTypeCollection {new MediaTypeHeaderValue("text/html")}; context.Result = new ObjectResult("<html><body>\r\n <h1>Internal Error Occured</h1><h2>We're sorry, we encountered an un-expected issue with your application.</h2> </body></html>\r\n") { StatusCode = 500, ContentTypes = mediaTypeCollection };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.