針對 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 新增至現有 Bot 的資訊,請參閱對話分析遙測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 (從 4.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 (從 4.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

從第一個查詢,選取一些作業識別碼並尋找更多資訊: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 中沒有來自您 Bot 的進一步事件,請檢查下列各項:If you are receiving 500 errors and there are no further events within Application Insights from your bot, check the following:

確保 Bot 在本機執行Ensure bot runs locally

請確定您的 bot 會先使用模擬器在本機執行。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 入口網站中開啟 Bot。Open your bot in the Azure Portal.
  2. 開啟 [App Service 設定 / 所有 App Service 設定] 頁面,以查看所有服務設定。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.

設定瀏覽器以重新啟動您的 Bot 服務Set up browser to restart your bot service

  1. 使用不同的瀏覽器,在 Azure 入口網站中開啟 Bot。Using a separate browser, open your bot in the Azure Portal.
  2. 開啟 [App Service 設定 / 所有 App Service 設定] 頁面,以查看所有服務設定。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.
    • 如果沒有任何活動,請重新部署您的 Bot。If there is no activity, redeploy your bot.
    • 然後切換到 [應用程式記錄] 頁面並尋找是否有任何錯誤。Then switch to the Application logs page and look for any errors.