首页 > 解决方案 > 如何在 azure 函数的堆栈跟踪中显示文件名和行号

问题描述

注意:如果有人对如何做到这一点有丝毫的想法,请写下答案:)

如何启用一个功能,以便在调试时在 azure 函数控制台窗口中显示的堆栈跟踪中捕获每个异常的文件名和行号。甚至更好的是,在调试 azure 函数时,如何在 Visual Studio 中触发调试断点?

我试过启用调试信息=完整,在项目设置->构建->高级->调试信息=完整,但这没有任何效果。

下面是一些代码,它们会生成一个索引越界异常来说明问题:

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

当我在调试模式下运行 azure 函数并遇到异常时,这是控制台窗口生成的输出:

[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').

这是项目中的 Host.json 文件:

在此处输入图像描述

标签: c#.net-coreazure-functionsstack-trace

解决方案


您只启用了 ApplicationInsight 日志。

将以下行添加到您的配置中

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

然后像下面这样修改日志记录配置

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();
    }
});

推荐阅读