你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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

Snapshot Debugger 当前适用于按 Windows 服务计划在 Azure Functions 上运行的 ASP.NET 和 ASP.NET Core 应用。

建议在使用 Snapshot Debugger 时,在“基本”服务层级或更高服务层级上运行应用程序。

对于大多数应用程序,“免费”和“共享”服务层没有足够的内存或磁盘空间来保存快照。

先决条件

在 Functions 应用中启用 Application Insights 监视

启用快照调试器

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

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

Snapshot Debugger 将作为 Azure Functions 运行时的一部分预安装,默认情况下处于禁用状态。 由于它包含在运行时中,因此无需添加额外的 NuGet 包或应用程序设置。

在下面的简单 .NET Core 函数应用示例中,.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 政府由世纪互联运行的 Microsoft Azure

以下示例显示使用美国政府云代理终结点更新的 host.json

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

以下是支持的 Snapshot Debugger 代理终结点的替代项:

属性 美国政府云 中国云
AgentEndpoint https://snapshot.monitor.azure.us https://snapshot.monitor.azure.cn

禁用快照调试器

若要在 Functions 应用中禁用 Snapshot Debugger,请通过将 snapshotConfiguration.isEnabled 属性设置为 false 来更新 host.json 文件。

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

我们建议在所有应用上启用快照调试器,以简化对应用程序异常的诊断。

后续步骤

  • 为应用程序生成可触发异常的流量。 然后等待 10 到 15 分钟,这样快照就会发送到 Application Insights 实例。
  • 在 Azure 门户中查看快照
  • 根据 Functions 应用上的用例来自定义 Snapshot Debugger 配置。 有关详细信息,请参阅 host.json 中的快照配置
  • 排查 Snapshot Debugger 问题时如需帮助,请参阅 Snapshot Debugger 故障排除