Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handling exceptions on ASP.NET Core web posting to Application Insights
// **********************************************
// Fore more information, visit:
// https://blog.hildenco.com/2020/03/adding-application-insights-telemetry.html
// **********************************************
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
// IExceptionHandlerPathFeature requires Microsoft.AspNetCore.Diagnostics
// run: dotnet add package Microsoft.AspNetCore.Diagnostics --version 2.2.0
var error = HttpContext.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
_telemetry.TrackException(error.Error);
}
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment