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 主机,然后 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"
      }
    }
  }
}

如果注意到任何部分缺少的日志,则可能是因为采样。 若要确定实际采样率,请使用使用以下代码片段中显示的所需时间间隔的分析查询。 如果发现 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 Monitor 使用的 IP 地址

联系我们寻求帮助

如果你有任何疑问或需要帮助,请创建支持请求联系 Azure 社区支持。 你还可以将产品反馈提交到 Azure 反馈社区