共用方式為


Azure Functions 應用程式的 Application Insights 記錄遺失或不正確

您可以透過 Azure FunctionsApplication Insights 之間的整合,密切監視函式應用程式。 而且您可以使用Application Insights,而不需要任何自定義組態。

如果 Application Insights 記錄遺失,或數據似乎部分或不正確,請使用下列步驟來解決問題。

檢查函式應用程式的設定

  1. Azure 入口網站中,瀏覽至您的函數應用程式。

  2. 選取 [診斷並解決問題],以開啟 [Azure Functions 診斷]

  3. 在 [搜尋] 列中,輸入 [函式組態檢查],然後開啟它。

  4. 您會看到所有函式應用程式組態檢查的診斷報告。 特別是 Application Insights,會執行下列檢查:

    • 只有下列其中一個連線設定存在:

      • APPINSIGHTS_INSTRUMENTATIONKEY Application Insights 檢測密鑰

      • APPLICATIONINSIGHTS_CONNECTION_STRING 連接

        建議您使用 APPLICATIONINSIGHTS_CONNECTION_STRING ,以取得更穩定的行為。 使用的能力 APPINSIGHTS_INSTRUMENTATIONKEY 將在 2025 年淘汰。

    • 按照建議,AzureWebJobsDashboard 內建記錄已停用。

    • Azure Functions 遙測已啟用取樣 (預設為啟用)。

建議:函式應用程式應位於第 4 版,且運行時間版本應至少為 4.15.2xx。 這是因為,從這個版本開始,您可以追蹤從 Azure Functions 到 Application Insights 服務的記錄流程。 藉由監視記錄流程,您可以檢查是否有遺漏的記錄。

自訂應用程式記錄檔

根據預設,您寫入的自訂應用程式記錄會傳送至 Functions 主機,然後主機會將記錄傳送至 Application Insights 底下的「背景工作角色」類別。 不過,某些語言堆疊可讓您將記錄直接傳送至 Application Insights,讓您完全掌控寫入記錄的發出方式。 在此案例中,記錄管線會從 worker > Functions host > Application Insights 變更為 worker > Application Insights

下表摘要列出每個堆疊可以使用的設定選項:

語言堆疊 設定自訂記錄的位置
.NET (內含式模型) host.json
.NET (隔離式模型) 預設 (將自訂記錄傳送至 Functions 主機):host.json
若要將記錄直接傳送至 Application Insights,請參閱 HostBuilder 中的設定 Application Insights。
Node.JS host.json
Python host.json
Java 預設 (將自訂記錄傳送至 Functions 主機):host.json
若要將記錄直接傳送至 Application Insights,請參閱 設定 Application Insights Java 代理程式
PowerShell host.json

設定為直接傳送自訂應用程式記錄時,主機不會再發出這些記錄,且 host.json 不再控制其行為。 同樣地,每個堆疊所公開的選項只會套用至自訂記錄,且不會變更本文所述的其他執行階段記錄行為。 在此案例中,若要控制所有記錄的行為,您可能需要變更這兩種設定。

記錄遺失或部分

Application Insights 會收集記錄、效能和錯誤資料。 取樣組態 可用來減少遙測量。 根據預設,取樣功能會啟用下列host.json範例所示的設定。 未取樣排除的類型。

{
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "maxTelemetryItemsPerSecond" : 20,
        "excludedTypes": "Request;Exception"
      }
    }
  }
}

如果您注意到任何部分遺漏的記錄,這可能是因為取樣而發生。 若要判斷實際的取樣率,請使用使用下列代碼段所示所需時間間隔的 Analytics 查詢。 如果您發現 TelemetrySavedPercentage 任何取樣類型的 小於 100,則會取樣該類型的遙測。

union requests,dependencies,pageViews,browserTimings,exceptions,traces
| where timestamp > todatetime("mm/dd/yyyy hh:mm:ss") and timestamp < todatetime("mm/dd/yyyy hh:mm:ss")
| summarize TelemetrySavedPercentage = 100/avg(itemCount), TelemetryDroppedPercentage = 100-100/avg(itemCount) by bin(timestamp, 1d), itemType
| sort by timestamp asc

如需詳細資訊,請參閱 Application Insights 中的資料收集、保留和儲存

控制記錄的磁碟區和詳細資訊

您可以增加或隱藏寫入的記錄。 若要這樣做,您可以使用記錄層級和類別的組合,如host.json中所設定。

Azure Functions 記錄器包括每個記錄的「類別」。 類別會指出運行時間程式代碼或函式程式代碼產生記錄檔的哪個部分。 例如:

  • Host.ResultsFunction.<YOUR_FUNCTION_NAME> 是一些可用的類別。
  • 記錄層級會指派給每個記錄檔。 值表示相對重要性,例如 WarningInformation

如需詳細資訊,請參閱其他可用的類別記錄層級

您可以遵循範例代碼段來設定應用程式應該如何撰寫記錄:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "Information", // catch all default, with modifications below for individual categories.
      "Function": "Warning", // Warning level from all Functions (except the ones configured below).
      "Host.Aggregator": "Trace", // Log all traces in the 'customMetrics' table of (and shown on Metrics/Alerts blade in AI) - use either this or Host.Results
      "Host.Results": "Error", // Error and Critical requests are only logged in the 'requests' table of the AI (and shown on Monitor Functions blade in Functions App) - use either this or Host.Aggregator
      "Function.Function1": "Information", //Information level logs from Function 1, logged in 'traces', 'dependencies' and 'customMetrics' tables of AI
      "Function.Function2.User": "Information" //user code logs from Function2, logged in 'traces' table of AI
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "maxTelemetryItemsPerSecond": 1,
        "excludedTypes": "Exception"
      }
    }
  }
}

若要在應用程式設定層級設定這些值(為了避免在host.json變更時重新部署),請建立對等值做為應用程式設定來覆寫特定的host.json值。 如需詳細資訊,請參閱覆寫 host.json 值

如需有關如何隱藏記錄的更多範例,請參閱 functions-log-suppress

虛擬網路整合函式應用程式不會產生任何記錄

如果函式應用程式與虛擬網路整合,您必須針對伺服器防火牆中的連出流量開啟埠 443,以允許 Application Insights SDK 或 Application Insights 代理程式將數據傳送至入口網站,以取得下列 URL:

  • dc.applicationinsights.azure.com
  • dc.applicationinsights.microsoft.com
  • dc.services.visualstudio.com
  • * .in.applicationinsights.azure.com

如需詳細資訊,請參閱 Azure 監視器所使用的 IP 位址

與我們連絡,以取得說明

如果您有問題或需要相關協助,請建立支援要求,或詢問 Azure community 支援。 您也可以向 Azure 意見反應社群提交產品意見反應。