Share via


在 Azure Functions 中啟用適用於 .NET 和 .NET Core 應用程式的快照偵錯工具

快照偵錯工具目前適用於在 Windows 服務方案上 Azure Functions 上執行的 ASP.NET 和 ASP.NET Core 應用程式。

建議您使用快照偵錯工具時,於基本服務層或更高層執行您的應用程式。

對於大部分應用程式,免費和共用服務層級沒有足夠的記憶體或磁碟空間來儲存快照集。

必要條件

在您的函數應用程式中啟用 Application Insights 監視

啟用快照偵錯工具

若要在函式應用程式中啟用快照偵錯工具,請將 snapshotConfiguration 屬性新增至 host.json 檔案,並重新部署函式。 例如:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "snapshotConfiguration": {
        "isEnabled": true
      }
    }
  }
}

快照偵錯工具會在 Azure Functions 執行階段過程中預先安裝,並預設為停用。 因為其包含在執行階段中,因此您不需要新增額外的 NuGet 套件或應用程式設定。

在下列簡單 .NET Core 函式應用程式中,.csproj{Your}Function.cshost.json 已啟用快照偵錯工具:

Project.csproj

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.31" />
</ItemGroup>
<ItemGroup>
    <None Update="host.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
</ItemGroup>
</Project>

{Your}Function.cs

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace SnapshotCollectorAzureFunction
{
    public static class ExceptionFunction
    {
        [FunctionName("ExceptionFunction")]
        public static Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            throw new NotImplementedException("Dummy");
        }
    }
}

Host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      },
      "snapshotConfiguration": {
        "isEnabled": true
      }
    }
  }
}

啟用其他雲端的快照偵錯工具

目前,唯一需要端點修改的區域是 Azure Government由 21Vianet 營運的 Microsoft Azure

下列是使用美國政府雲端代理程式端點更新顯示的 host.json 範例:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      },
      "snapshotConfiguration": {
        "isEnabled": true,
        "agentEndpoint": "https://snapshot.monitor.azure.us"
      }
    }
  }
}

這些是快照偵錯工具代理程式端點支援的覆寫:

屬性 美國政府雲端 中國雲端
AgentEndpoint https://snapshot.monitor.azure.us https://snapshot.monitor.azure.cn

停用快照偵錯工具

若要停用函數應用程式中的快照偵錯工具,請藉由將 snapshotConfiguration.isEnabled 屬性設定為 false 來更新 host.json 檔案。

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "snapshotConfiguration": {
        "isEnabled": false
      }
    }
  }
}

建議您在所有應用程式上啟用快照偵錯工具,以簡化應用程式例外狀況的診斷。

下一步

  • 針對應用程式產生可觸發例外狀況的流量。 然後等待 10 到 15 分鐘,讓快照集傳送到 Application Insights 執行個體。
  • 在 Azure 入口網站檢視快照集
  • 根據您的函數應用程式使用案例來自訂快照偵錯工具組態。 如需詳細資訊,請參閱 host.json 中的快照集組態
  • 如需針對快照偵錯工具問題進行疑難排解的說明,請參閱快照偵錯工具疑難排解