您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

启用 Azure Functions 中的 .NET 和 .NET Core 应用的 Snapshot DebuggerEnable Snapshot Debugger for .NET and .NET Core apps in Azure Functions

Snapshot Debugger 当前适用于在 Windows 服务计划的 Azure Functions 上运行的 ASP.NET 和 ASP.NET Core 应用。Snapshot Debugger currently works for ASP.NET and ASP.NET Core apps that are running on Azure Functions on Windows Service Plans.

建议你在使用 Snapshot Debugger 时,使用基本服务层或更高级别运行应用程序。We recommend you run your application on the Basic service tier or higher when using Snapshot Debugger.

对于大多数应用程序,免费和共享服务层没有足够的内存或磁盘空间来保存快照。For most applications, the Free and Shared service tiers don't have enough memory or disk space to save snapshots.

先决条件Prerequisites

启用快照调试器Enable Snapshot Debugger

如果运行的是其他类型的 Azure 服务,请参阅以下说明,了解如何在其他受支持的平台上启用 Snapshot Debugger:If you're running a different type of Azure service, here are instructions for enabling Snapshot Debugger on other supported platforms:

若要在 Function app 中启用 Snapshot Debugger,必须 host.json 通过添加下面定义的属性来更新文件, snapshotConfiguration 然后重新部署函数。To enable Snapshot Debugger in your Function app, you have to update your host.json file by adding the property snapshotConfiguration as defined below and redeploy your function.

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

Snapshot Debugger 作为 Azure Functions 运行时的一部分预安装,这在默认情况下处于禁用状态。Snapshot Debugger is pre-installed as part of the Azure Functions runtime, which by default it's disabled.

由于 Snapshot Debugger 包含在 Azure Functions 运行时中,因此不需要添加额外的 NuGet 包或应用程序设置。Since Snapshot Debugger it's included in the Azure Functions runtime, it isn't needed to add extra NuGet packages nor application settings.

就像引用一样,对于简单的函数应用 ( .NET Core) ,下面是它在 .csproj {Your}Function.cs host.json Snapshot Debugger 上启用、和后的外观。Just as reference, for a simple Function app (.NET Core), below is how it will look the .csproj, {Your}Function.cs, and host.json after enabled Snapshot Debugger on it.

项目 .csprojProject 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>

Function 类Function class

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 file

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

禁用快照调试器Disable Snapshot Debugger

若要在 Function app 中禁用 Snapshot Debugger,只需 host.json 通过将设置为属性来更新文件 false snapshotConfiguration.isEnabledTo disable Snapshot Debugger in your Function app, you just need to update your host.json file by setting to false the property snapshotConfiguration.isEnabled.

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

建议你在所有应用上都启用了 Snapshot Debugger,以方便诊断应用程序异常。We recommend you have Snapshot Debugger enabled on all your apps to ease diagnostics of application exceptions.

后续步骤Next steps

  • 为应用程序生成可触发异常的流量。Generate traffic to your application that can trigger an exception. 然后等待 10 到 15 分钟,这样快照就会发送到 Application Insights 实例。Then, wait 10 to 15 minutes for snapshots to be sent to the Application Insights instance.
  • 查看 Azure 门户中的快照。View snapshots in the Azure portal.
  • 基于函数应用上的用例自定义 Snapshot Debugger 配置。Customize Snapshot Debugger configuration based on your use-case on your Function app. 有关详细信息,请参阅 中 host.js的快照配置For more info, see snapshot configuration in host.json.
  • 排查 Snapshot Debugger 问题时如需帮助,请参阅 Snapshot Debugger 故障排除For help with troubleshooting Snapshot Debugger issues, see Snapshot Debugger troubleshooting.