簡體   English   中英

如何在 azure function 的堆棧跟蹤中顯示文件名和行號

[英]How to show filename and line number in stacktrace in azure function

注意:如果有人對如何做到這一點有絲毫的想法,請寫一個答案:)

如何啟用一個功能,以便在調試時在 azure function 控制台 window 中顯示的堆棧跟蹤中捕獲每個異常的文件名和行號。 或者更好的是,在調試 azure function 時,如何在 Visual Studio 中觸發調試斷點?

我試過啟用調試信息=完整,在項目設置->構建->高級->調試信息=完整,但這沒有任何效果。

下面是一些代碼,它們會生成一個索引越界異常來說明問題:

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

當我在調試模式下運行 azure function 並遇到異常時,這是控制台生成的 output:

[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 日志。

將以下幾行添加到您的配置中

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

您必須在您的機器中將環境變量 CLI_DEBUG 設置為 1。

https://stackoverflow.com/a/73277578

我現在可以在 Visual Studio 調試器中看到文件名。 您需要在 Visual Studio 的異常設置 window 中啟用不同的異常。 我不確定具體是哪一個,所以我只啟用每個“公共語言運行時異常”。 像這樣: https://i.stack.imgur.com/zJGcS.png

您必須在您的機器中將環境變量 CLI_DEBUG 設置為 1 才能查看拋出的異常中的文件名和行號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM