Thursday, September 28, 2017

How to trace errors in asp.net core

Leave a Comment

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?

2 Answers

Answers 1

Nobody store any "details" for you.

This is just an identifier that you should pass between classes/services processing user request, and when each service needs to log something, it can include this ID, thereby ensuring that you can (later) track user request in logs from start to finish.

Answers 2

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(); } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment