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

排查 HTTP 500 内部服务错误

适用于:SDK v4

排查 500 错误的第一步是启用 Application Insights。

有关 AppInsights 示例,请参阅 luis-with-appinsights C# 示例JS 示例

注意

语言理解(LUIS)将于 2025 年 10 月 1 日停用。 2023 年 4 月 1 日起,将无法创建新的 LUIS 资源。 较新版本的语言理解功能现已作为 Azure AI 语言的一部分提供。

对话语言理解(CLU)是 Azure AI 语言的一项功能,同时也是更新版的 LUIS。 有关 Bot Framework SDK 语言理解支持的详细信息,请参阅自然语言理解

请参阅聊天分析遥测,了解如何向现有的机器人添加 Application Insights。

为 ASP.NET 启用 Application Insights

如需基本的 Application Insights 支持,请参阅如何为 ASP.NET 网站设置 Application Insights。 Bot Framework(从 v4.2 开始)提供另一级别的 Application Insights 遥测,但它不是诊断 HTTP 500 错误所必需的。

为 Node.js 启用 Application Insights

如需基本的 Application Insights 支持,请参阅如何使用 Application Insights 监视 Node.js 服务和应用。 Bot Framework(从 v4.2 开始)提供另一级别的 Application Insights 遥测,但它不是诊断 HTTP 500 错误所必需的。

查询异常

若要分析 HTTP 状态代码 500 错误,最简单的方法是从异常开始。

以下查询会告知你最新的异常:

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

从第一个查询中选择一些操作 ID,查找更多信息:

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,请分析详细信息,看其是否对应于代码中的行。 如果只看到来自通道连接器 (Microsoft.Bot.ChannelConnector) 的异常,则请参阅无 Application Insights 事件,确保 Application Insights 已正确设置且代码在记录事件。

无 Application Insights 事件

如果收到 500 错误且 Application Insights 中没有来自机器人的更多事件,则请检查以下事项:

确保机器人在本地运行

首先,使用 Bot Framework Emulator 在本地测试机器人。

确保正在复制配置文件(仅限 .NET)

确保在部署过程中将 appsettings.json 和任何其他的配置文件正确打包。

应用程序集

确保在部署过程中将 Application Insights 程序集正确打包。

  • Microsoft.ApplicationInsights
  • Microsoft.ApplicationInsights.TraceListener
  • Microsoft.AI.Web
  • Microsoft.AI.WebServer
  • Microsoft.AI.ServeTelemetryChannel
  • Microsoft.AI.PerfCounterCollector
  • Microsoft.AI.DependencyCollector
  • Microsoft.AI.Agent.Intercept

确保在部署过程中将 appsettings.json 和任何其他的配置文件正确打包。

appsettings.json

appsettings.json 文件中,确保检测密钥已设置。

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

验证配置文件

确保配置文件中包含一个 Application Insights 密钥。

{
    "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": ""
    }
},

检查日志

Bot ASP.NET 和 Node 会在服务器级别发出可以检查的日志。

设置一个用于查看日志的浏览器

  1. Azure 门户中打开机器人。
  2. 打开“应用服务设置/所有应用服务设置”页,查看所有服务设置。
  3. 打开应用服务的“监视/诊断日志”页。
    • 将“应用程序日志记录(文件系统)”已启用。 如果更改此设置,请确保单击“保存”。
  4. 切换到“监视/日志流”页。
    • 选择 Web 服务器日志,确保看到一条指示已连接的消息。 它应该如下所示:

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

      让此窗口保持打开状态。

设置浏览器,以便重启机器人服务

  1. 使用单独的浏览器,在 Azure 门户中打开机器人。
  2. 打开“应用服务设置/所有应用服务设置”页,查看所有服务设置。
  3. 切换到应用服务的“概览”页,单击“重启”。
    • 系统会提示是否确定执行该操作,此时请选择
  4. 返回到第一个浏览器窗口,查看日志。
  5. 验证是否收到新日志。
    • 如果没有任何活动,请重新部署机器人。
    • 然后切换到“应用程序日志”页,查看是否存在任何错误。

后续步骤