The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
In ASP.NET Core, error logging is done using ILogger. It helps to save errors in file, database, console, or logging tools.
1. Use ILogger in Controller
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
try
{
int x = 10;
int y = 0;
int z = x / y;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred in Index");
}
return View();
}
}
In .NET Core we log errors using ILogger. It supports multiple log providers like Console, File, Serilog, NLog, etc. We use _logger.LogError(ex, "message") inside try-catch or middleware.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In ASP.NET Core, error logging is done using ILogger.
It helps to save errors in file, database, console, or logging tools.
1. Use ILogger in Controller
This will log error in console / default logger.
2. Log in Middleware / API
3. Log Levels
Example:
4. Use Serilog (Production)
Most used logging tool with .NET Core
Example:
Short Answer for Interview: