在 Azure Functions 中为 .NET 和 .NET Core 应用启用 Snapshot Debugger

Snapshot Debugger 目前适用于在 Windows 服务计划中在 Azure Functions 上运行的 ASP.NET 和 ASP.NET 核心应用。

使用 Snapshot Debugger 时,请在基本或更高服务层级上运行应用程序。 对于大多数应用程序:

  • “免费”和“共享”服务层级没有足够的内存或磁盘空间用于保存快照。
  • 消耗层当前不可用于 Snapshot Debugger。

快照调试器已经作为 Azure Functions 运行时的一部分被预安装。 无需添加额外的 NuGet 包或应用程序设置。

先决条件

在启用 Snapshot Debugger 之前,请先在函数应用中启用 Application Insights 监视功能。 快照调试器依赖于 Application Insights 来存储和显示捕获的快照。

启用快照调试器

若要在函数应用中启用 Snapshot Debugger,请将 snapshotConfiguration 属性添加到 host.json 文件并重新部署函数。 例如:

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

生成触发异常的应用程序的流量。 然后等待 10 到 15 分钟,让快照传送到 Application Insights 实例。

若要验证快照调试器是否已启用,请检查 .NET 函数应用文件。 例如,在以下简单的 .NET 函数应用中,.NET 应用程序的 .csproj{Your}Function.cshost.json 显示 Snapshot Debugger 已启用:

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
      }
    }
  }
}

为其他云启用 Snapshot Debugger

目前,由世纪互联运营的 Azure 区域需要对终结点进行修改。

以下示例演示了由世纪互联运营的 Azure 云代理端点更新的 host.json:

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

下表列出了 Snapshot Debugger 代理端点支持的替代项:

属性 由世纪互联运营的 Azure
agentEndpoint https://snapshot.monitor.azure.cn

禁用快照调试器

若要在函数应用中禁用 Snapshot Debugger,请更新 false 文件,将 snapshotConfiguration.isEnabled 属性设置为

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