How to show filename and line number in stacktrace in azure function

Viewed 515

ATTENTION: If anyone have even the slightest idea of how to accomplish this, please write an answer :)

How can I enable a feature such that filename and line number of each exception is captured in the stacktrace displayed in the azure function console window when debugging. Or even better, how can I hit a debug breakpoint inside Visual Studio when debugging an azure function?

I have tried enabling debugging information = full, inside project settings -> build -> advanced -> debugging information = full, but this did not have any effect.

Here is some code that will generate an index out of bounds exception which showcases the problem:

List<string> test = new List<string>();
        Console.WriteLine(test[0]);

When I run the azure function in debug mode and hit the exception this is the generated output of the console window:

[2020-10-02T13:01:38.289] Executed 'Function1' (Failed, Id=180bf39f-1b60-401e-80a4-2073de926e9e, Duration=340ms)
[2020-10-02T13:01:38.290] System.Private.CoreLib: Exception while executing function: Function1. System.Private.CoreLib: One or more errors occurred. (Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')). System.Private.CoreLib: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index').

Here is the Host.json file from the project:

enter image description here

3 Answers

you only enabled ApplicationInsight Logs.

add below lines into your config

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

then modify logging configuration like below

services.AddLogging(config =>
{
    // clear out default configuration ==> if necessary
    config.ClearProviders();

    config.AddConfiguration(Configuration.GetSection("Logging"));
    config.AddDebug();
    
    if(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == EnvironmentName.Development) 
    {
        config.AddConsole();
    }
});

I can now see the filename in Visual Studio debugger. You need to enable different exceptions in Exception Settings window in Visual Studio. I am not sure specifically which one so I just enable every "Common Language Runtime Exceptions". Like this: https://i.stack.imgur.com/zJGcS.png

You have to setup the env variable CLI_DEBUG to 1 in your machine for seeing the filename and line number in the exceptions thrown.

Related