Reputation: 1867
I am new to asp.net core mvc. i need to write Global exception handling and error Logging in on palce..i dont need to write all below in every controller Please help me to write globally in one place
Here is the my code
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Logging;
namespace NS.Controllers
{
public class HomeController : Controller
{
static ILogger _logger;
public HomeController(ILoggerFactory factory)
{
if (_logger == null)
_logger = factory.Create("Unhandled Error");
}
public IActionResult Error()
{
var feature = HttpContext.Features.Get<IExceptionHandlerFeature>();
var error = feature?.Error;
_logger.LogError("Oops!", error);
return View("~/Views/Shared/Error.cshtml", error);
}
}
}
Upvotes: 1
Views: 3074
Reputation: 6203
In Startup.cs
using .UseExceptionHandler()
you can set route to your error action:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseExceptionHandler("/error");
}
Here you find more : MSDN
Upvotes: 1