Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error management in ASP.NET Core using a BaseController

In previous ASP.NET versions I was used to create a BaseController inherited from the other controllers and there intercepting the general error and logging each error with a simple logging method and passing the ExceptionContext filterContext.

Should I do the same in ASP.NET 5?

I see in file Startup.cs that there is a if/else statement that basically separate the debug/live condition, with line

app.UseErrorHandler("/Home/Error");

for a production application.

How am I supposed to hook in the process and log the errors?

like image 381
Francesco Cristallo Avatar asked Nov 16 '14 13:11

Francesco Cristallo


People also ask

How do you handle exceptions globally in .NET Core?

The middleware UseExceptionHandler can be used to handle exceptions globally. You can get all the details of the exception object (Stack Trace, Inner exception, message etc..) and display them on-screen. You can implement like this.

What is Handleerror attribute used for?

The HandleErrorAttribute attribute in ASP.NET MVC lets you specify how to handle an exception that is thrown by an action method. By default, when an action method with the HandleErrorAttribute attribute throws any exception, MVC displays the Error view that is located in the ~/Views/Shared folder.


1 Answers

Handling and logging errors in ASP.NET 5 involves a few elements.

To handle the error in a production scenario and show an error page, the app.UseExceptionHandler() method is the way to go. The Diagnostics repository on the ASP.NET GitHub organisation includes a sample that shows this. For development-time scenarios where you want to see a full stack trace and other diagnostic information, use app.UseDeveloperExceptionPage() as seen in this sample.

If the application is using ASP.NET MVC 6, then there is an MVC-specific way of handling errors, much as there was in earlier versions of ASP.NET MVC. In ASP.NET MVC 6, a filter (or the Controller itself, which is also a filter) can handle/override the OnActionExecuted method and inspect the ActionExecutedContext parameter to attempt to handle the error.

When it comes to logging, there's a new logging infrastructure in ASP.NET 5 that reports a great deal of information to any registered ILogger. The default project templates in Visual Studio 2015 register some loggers that log errors (and other data) to the console and the Visual Studio debug output window. However, when running in IIS or IIS Express there is no console window (yet!). But if you run from the command line (using dnx web) then you'll see the error. Or, of course, you can register a different logger that writes to a log file or database and see the logs there.

To register a custom ILogger in a web app:

  1. Write a logger that implements the ILogger interface. See the DNX implementations for how to do this.
  2. Add a parameter of type ILoggerFactory to your app's Startup class's Configure method.
  3. In the Configure method call loggerFactory.AddProvider(<some provider>) and pass in an instance of your logger.
like image 200
Eilon Avatar answered Oct 23 '22 15:10

Eilon