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

Anass Kartit
  • 2,017
  • 15
  • 22
Fabricio Rodriguez
  • 3,769
  • 11
  • 48
  • 101
  • did you try this : https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions#exceptions? are you catching and tracing? – Anass Kartit Oct 14 '19 at 12:21
  • Thanks Anass. I saw that, but further down in the documentation, I saw this https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions#web-api 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... – Fabricio Rodriguez Oct 14 '19 at 12:26
  • Having the same exact issue. @FabricioRodriguez - did you ever figure out how to get the stack trace / exception details? – Mike Smith Dec 29 '19 at 05:39

3 Answers3

10

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
Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63
  • This is the most awesomest query ever. Thank you @Ε Г И І И О – KramFfud Jul 12 '21 at 04:14
  • 1
    awesome query - FYI - you can use [`coalesce`](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/coalescefunction) 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. – KyleMit Aug 05 '21 at 20:21
2

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

Anass Kartit
  • 2,017
  • 15
  • 22
  • Thanks Anass. I replied to your comment on my original post: "I saw that, but further down in the documentation, I saw this https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions#web-api 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" – Fabricio Rodriguez Oct 14 '19 at 12:28
  • did you see this https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core – Anass Kartit Oct 14 '19 at 12:29
  • I think I am using Application Insights version 9.1.913.1, for the record. – Fabricio Rodriguez Oct 14 '19 at 12:29
  • Aaaah, no I did not see that. Thank you! This might be the answer. Going through it now. Will let you know shortly... – Fabricio Rodriguez Oct 14 '19 at 12:30
  • might be a nuget issue, try debugging using visual studio and creating an exception manually to see first – Anass Kartit Oct 14 '19 at 12:31
  • 1
    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... – Fabricio Rodriguez Oct 14 '19 at 12:47
  • from your screen shots, you have to see the Top 3 exceptions but you are not seeing those? – Anass Kartit Oct 14 '19 at 12:54
  • 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. – Fabricio Rodriguez Oct 14 '19 at 12:58
1

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.

Fabricio Rodriguez
  • 3,769
  • 11
  • 48
  • 101
  • 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). – ZakiMa Oct 15 '19 at 04:46
  • Can you please share more details how you instrumented your application? – ZakiMa Oct 15 '19 at 04:47
  • If correlation worked you would see similar picture to this: https://learn.microsoft.com/en-us/azure/azure-monitor/app/transaction-diagnostics#transaction-diagnostics-experience – ZakiMa Oct 15 '19 at 04:47