问题
I've got an ASP .Net Core 3.0 Web API hosted on Azure App Service. I'm am trying to figure out why it's throwing a 500 Internal Server Error in one of the controller action methods. I've got Application Insights set up, and I can see on the "Failures" page on Azure Portal that there are a number of 500 exceptions. However, I cannot see a stack trace for them. Is there something I need to do to turn on stack trace reporting in Application Insights or Azure Monitor. P.S. Even when my API was on .Net Core 2.2, it also wasn't showing stack traces, so it's not a .Net Core 3.0 thing.
Here's some screenshots:
回答1:
If you are not seeing the stack trace you have to make sure your code logs the exceptions in one of the ways described in here:
https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions#exceptions
in MVC you have to use this:
public override void OnException(ExceptionContext filterContext)
{
if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
{
//If customError is Off, then AI HTTPModule will report the exception
if (filterContext.HttpContext.IsCustomErrorEnabled)
{ //or reuse instance (recommended!). see note above
var ai = new TelemetryClient();
ai.TrackException(filterContext.Exception);
}
}
base.OnException(filterContext);
}
in .net core it is done at the configureservice level:
public void ConfigureServices(IServiceCollection services)
{
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions aiOptions
= new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
// Disables adaptive sampling.
aiOptions.EnableAdaptiveSampling = false;
// Disables QuickPulse (Live Metrics stream).
aiOptions.EnableQuickPulseMetricStream = false;
services.AddApplicationInsightsTelemetry(aiOptions);
}
as described in here:
https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core
回答2:
I figured that looking at "Failed Requests" in the "Operations" tab of the "Failures" page of Application Insights does not show a stack trace. I guess this is because this section has to do with HTTP requests and nothing more. But if I instead switch to the Exceptions tab, I can see the stack trace there. I guess this is the section that relates to the execution of the code, and that's why it has a stack trace.
来源:https://stackoverflow.com/questions/58376320/azure-monitor-application-insights-not-showing-stack-trace-for-errors