Skip to content

Instantly share code, notes, and snippets.

@hd9
Last active February 18, 2020 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hd9/086ab91c02b856449aeb7e7ead4b7a38 to your computer and use it in GitHub Desktop.
Save hd9/086ab91c02b856449aeb7e7ead4b7a38 to your computer and use it in GitHub Desktop.
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