Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trace errors in asp.net core

Tags:

asp.net-core

I noticed that when you create a default asp.net core project in visual studio, there is an Error action that looks like this:

    public IActionResult Error()
    {
        ViewData["RequestId"] = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
        return View();
    }

The error page shows that RequestId properly, however, I don't know how to check the details of that error if my user sends me a screenshot of that error. Where is this stored?

like image 329
user3900456 Avatar asked Jan 03 '23 10:01

user3900456


1 Answers

ExceptionHandlerMiddleware stores the exception in HttpContext as an ExceptionHandlerFeature.

So in the Error Action you can get the error message by doing something like this,

public IActionResult Error()
{
    var ehf = HttpContext.Features.Get<IExceptionHandlerFeature>();
    ViewData["ErrorMessage"] = ehf.Error.Message;

    return View();
}
like image 162
Laksmono Avatar answered Jan 29 '23 14:01

Laksmono