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

排查 HTTP 500 错误Troubleshoot HTTP 500 errors

适用于: SDK v4APPLIES TO: SDK v4

排查 500 错误的第一步是启用 Application Insights。The first step in troubleshooting 500 errors is enabling Application Insights.

对于 AppInsights 示例,请参阅 luis AppInsights c # 示例JS 示例For AppInsights samples, see the luis-with-appinsights C# sample and JS sample.

请参阅聊天分析遥测,了解如何向现有的机器人添加 Application Insights。See conversation analytics telemetry for information about how to add Application Insights to an existing bot.

在 ASP.Net 上启用 Application InsightsEnable Application Insights on ASP.Net

如需基本的 Application Insights 支持,请参阅如何为 ASP.NET 网站设置 Application InsightsFor basic Application Insights support, see how to set up Application Insights for your ASP.NET website. Bot Framework(从 v4.2 开始)提供另一级别的 Application Insights 遥测,但它不是诊断 HTTP 500 错误所必需的。The Bot Framework (starting with v4.2) provides an additional level of Application Insights telemetry, but it is not required for diagnosing HTTP 500 errors.

在 Node.js 上启用 Application InsightsEnable Application Insights on Node.js

如需基本的 Application Insights 支持,请参阅如何使用 Application Insights 监视 Node.js 服务和应用For basic Application Insights support, see how to monitor your Node.js services and apps with Application Insights. Bot Framework(从 v4.2 开始)提供另一级别的 Application Insights 遥测,但它不是诊断 HTTP 500 错误所必需的。The Bot Framework (starting with v4.2) provides an additional level of Application Insights telemetry, but it is not required for diagnosing HTTP 500 errors.

查询异常Query for exceptions

若要分析 HTTP 状态代码 500 错误,最简单的方法是从异常开始。The easiest method of analyzing HTTP status code 500 errors is to begin with exceptions.

以下查询会告知你最新的异常:The following queries will tell you the most recent exceptions:

exceptions
| order by timestamp desc
| project timestamp, operation_Id, appName

从第一个查询中选择一些操作 ID,查找更多信息:From the first query, select a few of the operation IDs and look for more information:

let my_operation_id = "d298f1385197fd438b520e617d58f4fb";
let union_all = () {
    union
    (traces | where operation_Id == my_operation_id),
    (customEvents | where operation_Id == my_operation_id),
    (requests | where operation_Id == my_operation_id),
    (dependencies | where operation_Id  == my_operation_id),
    (exceptions | where operation_Id == my_operation_id)
};

union_all
    | order by timestamp desc

如果只有 exceptions,请分析详细信息,看其是否对应于代码中的行。If you have only exceptions, analyze the details and see if they correspond to lines in your code. 如果只看到来自通道连接器 (Microsoft.Bot.ChannelConnector) 的异常,则请参阅无 Application Insights 事件,确保 Application Insights 已正确设置且代码在记录事件。If you only see exceptions coming from the Channel Connector (Microsoft.Bot.ChannelConnector) then see No Application Insights events to ensure that Application Insights is set up correctly and your code is logging events.

无 Application Insights 事件No Application Insights events

如果收到 500 错误且 Application Insights 中没有来自机器人的更多事件,则请检查以下事项:If you are receiving 500 errors and there are no further events within Application Insights from your bot, check the following:

确保机器人在本地运行Ensure bot runs locally

请确保机器人首先在本地运行模拟器。Make sure your bot runs locally first with the Emulator.

确保正在复制配置文件(仅限 .NET)Ensure configuration files are being copied (.NET only)

确保在部署过程中将 appsettings.json 和任何其他的配置文件正确打包。Make sure your appsettings.json and any other configuration files are being packaged correctly during the deployment process.

应用程序集Application assemblies

确保在部署过程中将 Application Insights 程序集正确打包。Ensure the Application Insights assemblies are being packaged correctly during the deployment process.

  • Microsoft.ApplicationInsightsMicrosoft.ApplicationInsights
  • Microsoft.ApplicationInsights.TraceListenerMicrosoft.ApplicationInsights.TraceListener
  • Microsoft.AI.WebMicrosoft.AI.Web
  • Microsoft.AI.WebServerMicrosoft.AI.WebServer
  • Microsoft.AI.ServeTelemetryChannelMicrosoft.AI.ServeTelemetryChannel
  • Microsoft.AI.PerfCounterCollectorMicrosoft.AI.PerfCounterCollector
  • Microsoft.AI.DependencyCollectorMicrosoft.AI.DependencyCollector
  • Microsoft.AI.Agent.InterceptMicrosoft.AI.Agent.Intercept

确保在部署过程中将 appsettings.json 和任何其他的配置文件正确打包。Make sure your appsettings.json and any other configuration files are being packaged correctly during the deployment process.

appsettings.jsonappsettings.json

appsettings.json 文件中,确保检测密钥已设置。Within your appsettings.json file ensure the Instrumentation Key is set.

{
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information"
        },
        "Console": {
            "IncludeScopes": "true"
        }
    }
}

验证配置文件Verify config file

确保配置文件中包含一个 Application Insights 密钥。Ensure there's an Application Insights key included in your config file.

{
    "ApplicationInsights": {
        "type": "appInsights",
        "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "subscriptionId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "resourceGroup": "my resource group",
        "name": "my appinsights name",
        "serviceName": "my service name",
        "instrumentationKey": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "applicationId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "apiKeys": {},
        "id": ""
    }
},

检查日志Check logs

Bot ASP.Net 和 Node 会在服务器级别发出可以检查的日志。Bot ASP.Net and Node will emit logs at the server level that can be inspected.

设置一个用于查看日志的浏览器Set up a browser to watch your logs

  1. Azure 门户中打开机器人。Open your bot in the Azure Portal.
  2. 打开“应用服务设置/所有应用服务设置”页,查看所有服务设置。 Open the App Service Settings / All App service settings page to see all service settings.
  3. 打开应用服务的“监视/诊断日志”页。 Open the Monitoring / Diagnostics Logs page for the app service.
    • 将“应用程序日志记录(文件系统)”已启用。 Ensure that Application Logging (Filesystem) is enabled. 如果更改此设置,请确保单击“保存”。 Be sure to click Save if you change this setting.
  4. 切换到“监视/日志流”页。 Switch to the Monitoring / Log Stream page.
    • 选择“Web 服务器日志”,确保看到一条指示已连接的消息。 Select Web server logs and ensure you see a message that you are connected. 它应该如下所示:It should look something like the following:

      Connecting...
      2018-11-14T17:24:51  Welcome, you are now connected to log-streaming service.
      

      让此窗口保持打开状态。Keep this window open.

设置浏览器,以便重启机器人服务Set up browser to restart your bot service

  1. 使用单独的浏览器,在 Azure 门户中打开机器人。Using a separate browser, open your bot in the Azure Portal.
  2. 打开“应用服务设置/所有应用服务设置”页,查看所有服务设置。 Open the App Service Settings / All App service settings page to see all service settings.
  3. 切换到应用服务的“概览”页,单击“重启”。 Switch to the Overview page for the app service and click Restart.
    • 系统会提示你是否确定执行该操作,此时请选择“是” 。It will prompt if you are sure; select yes.
  4. 返回到第一个浏览器窗口,查看日志。Return to the first browser window and watch the logs.
  5. 验证是否收到新日志。Verify that you are receiving new logs.
    • 如果没有任何活动,请重新部署机器人。If there is no activity, redeploy your bot.
    • 然后切换到“应用程序日志”页,查看是否存在任何错误。 Then switch to the Application logs page and look for any errors.