【问题标题】:How to show filename and line number in stacktrace in azure function如何在 azure 函数的堆栈跟踪中显示文件名和行号
【发布时间】:2021-01-18 04:22:20
【问题描述】:

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

如何启用一项功能,以便在调试时在 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 文件:

【问题讨论】:

  • 我猜你需要使用applicationInsight
  • @DervişKayımbaşıoğlu 我会在将代码部署到 azure 时使用它,但我需要能够在本地开发并在调试时看到此信息:)
  • 您有用于日志记录的启动配置吗?你能把它添加到问题中吗?
  • @DervişKayımbaşıoğlu 我已将其添加到问题的底部。

标签: c# .net-core azure-functions stack-trace


【解决方案1】:

您只启用了 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();
    }
});

【讨论】:

  • 我已将日志记录行添加到 host.json 文件中,这会按预期产生大量调试输出,但它仍然不会显示发生异常的文件名和行号。 services.AddLogging() 配置仅在标准网络核心项目中的天蓝色函数中不存在,因此我无法启用它。
猜你喜欢
  • 1970-01-01
  • 2018-08-20
  • 2021-06-30
  • 2023-03-09
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多