Azure Monitor / Application Insights not showing stack trace for errors
Asked Answered
H

3

9

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:

enter image description here

enter image description here

Hostile answered 14/10, 2019 at 11:59 Comment(3)
did you try this : learn.microsoft.com/en-us/azure/azure-monitor/app/…? are you catching and tracing?Spirant
Thanks Anass. I saw that, but further down in the documentation, I saw this learn.microsoft.com/en-us/azure/azure-monitor/app/… which states "Starting with Application Insights Web SDK version 2.6 (beta3 and later), Application Insights collects unhandled exceptions thrown in the controller methods automatically for WebAPI 2+". Now I think this entire document is referring to the .Net Framework and not .Net Core anyway...Hostile
Having the same exact issue. @FabricioRodriguez - did you ever figure out how to get the stack trace / exception details?Watercraft
A
11

In app insights query window, write a query to display exceptions and project the property called "details". That contains the stack trace information.

 exceptions
| where timestamp > ago(30d)
| order by timestamp asc
| project timestamp, message = iff(message != '', message, iff(innermostMessage != '', innermostMessage, customDimensions.['prop__{OriginalFormat}'])), details
Ancon answered 6/7, 2021 at 18:43 Comment(2)
This is the most awesomest query ever. Thank you @Ε Г И І И ОGilbreath
awesome query - FYI - you can use coalesce to return the first variable with a value and cleanup the iff statements. Also some of those exception message properties may have changed as well.Zap
H
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.

Hostile answered 14/10, 2019 at 13:42 Comment(3)
Looks like correlation doesn't work. If it worked then exceptions would show up on Operations tab in "Top 3 Exceptions" grid, in End-to-end Transaction Details experience (both as separate line and as a part of request).Orman
Can you please share more details how you instrumented your application?Orman
If correlation worked you would see similar picture to this: learn.microsoft.com/en-us/azure/azure-monitor/app/…Orman
S
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://learn.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://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core

Spirant answered 14/10, 2019 at 12:26 Comment(8)
Thanks Anass. I replied to your comment on my original post: "I saw that, but further down in the documentation, I saw this learn.microsoft.com/en-us/azure/azure-monitor/app/… which states "Starting with Application Insights Web SDK version 2.6 (beta3 and later), Application Insights collects unhandled exceptions thrown in the controller methods automatically for WebAPI 2+". Now I think this entire document is referring to the .Net Framework and not .Net Core anyway"Hostile
did you see this learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-coreSpirant
I think I am using Application Insights version 9.1.913.1, for the record.Hostile
Aaaah, no I did not see that. Thank you! This might be the answer. Going through it now. Will let you know shortly...Hostile
might be a nuget issue, try debugging using visual studio and creating an exception manually to see firstSpirant
Ok, I figured out that looking at "Failed Requests" in the "Operations" tab of the "Failures" page of Azure Portal's Application Insights does not show a stack trace. But if I instead switch to the Exceptions tab, I can see the stack trace there... I think that is the correct place to go to view stack traces...Hostile
from your screen shots, you have to see the Top 3 exceptions but you are not seeing those?Spirant
Yeah, if i go to Operations, and then drill in, I see the 500 Internal Server Errors, but no stack trace. However, if I go to Exceptions, and drill into those, I see NullReferenceExceptions (which are the ones causing the 500 Internal Server Error under Operations) and those do have stack traces.Hostile

© 2022 - 2024 — McMap. All rights reserved.