监视 Azure FunctionsMonitor Azure Functions

Azure Functions 提供与 Azure Application Insights 的内置集成来监视函数。Azure Functions offers built-in integration with Azure Application Insights to monitor functions. 本文概述了 Azure 提供的用于监视 Azure Functions 的监视功能。This article provides an overview of the monitoring capabilities provided by Azure for monitoring Azure Functions.

Application Insights 可收集日志、性能和错误数据。Application Insights collects log, performance, and error data. 通过自动检测性能异常并使用强大的分析工具,你可以更轻松地诊断问题并更好地了解函数的使用方式。By automatically detecting performance anomalies and featuring powerful analytics tools, you can more easily diagnose issues and better understand how your functions are used. 这些工具设计用来帮助你持续提高函数的性能与可用性。These tools are designed to help you continuously improve performance and usability of your functions. 你甚至可以在本地函数应用项目开发过程中使用 Application Insights。You can even use Application Insights during local function app project development. 有关详细信息,请参阅什么是 Application Insights?For more information, see What is Application Insights?.

由于 Application Insights 检测内置于 Azure Functions 中,因此你只需要一个有效的检测密钥即可将函数应用连接到 Application Insights 资源。As Application Insights instrumentation is built into Azure Functions, you need a valid instrumentation key to connect your function app to an Application Insights resource. 在 Azure 中创建函数应用资源时,检测密钥会添加到应用程序设置中。The instrumentation key is added to your application settings as you create your function app resource in Azure. 如果函数应用还没有此密钥,则可以进行手动设置If your function app doesn't already have this key, you can set it manually.

Application Insights 定价和限制Application Insights pricing and limits

你可以免费试用 Application Insights 与 Azure Functions 的集成,但每天免费处理的数据量有限制。You can try out Application Insights integration with Azure Functions for free featuring a daily limit to how much data is processed for free.

如果在开发过程中启用 Application Insights,则在测试过程中可能会达到此限制。If you enable Applications Insights during development, you might hit this limit during testing. 当你达到每日限制时,Azure 会提供门户和电子邮件通知。Azure provides portal and email notifications when you're approaching your daily limit. 如果未收到这些警报并达到限制,则新日志不会显示在 Application Insights 查询中。If you miss those alerts and hit the limit, new logs won't appear in Application Insights queries. 请注意限制,以免在故障排除上花费不必要的时间。Be aware of the limit to avoid unnecessary troubleshooting time. 有关详细信息,请参阅在 Application Insights 中管理定价和数据量For more information, see Manage pricing and data volume in Application Insights.


Application Insights 具有采样功能,可以防止在峰值负载时为已完成的执行生成过多的遥测数据。Application Insights has a sampling feature that can protect you from producing too much telemetry data on completed executions at times of peak load. 采样功能默认处于启用状态。Sampling is enabled by default. 如果你看起来缺少数据,则可能需要调整采样设置来适应你的具体监视方案。If you appear to be missing data, you might need to adjust the sampling settings to fit your particular monitoring scenario. 若要了解详细信息,请参阅配置采样To learn more, see Configure sampling.

适用于 Azure Functions 的 Application Insights 支持的功能中详细介绍了函数应用可用的 Application Insights 功能的完整列表。The full list of Application Insights features available to your function app is detailed in Application Insights for Azure Functions supported features.

Application Insights 集成Application Insights integration

通常,你在创建函数应用时创建 Application Insights 实例。Typically, you create an Application Insights instance when you create your function app. 在本例中,集成所需的检测密钥已设置为名为 APPINSIGHTS_INSTRUMENTATIONKEY 的应用程序设置。In this case, the instrumentation key required for the integration is already set as an application setting named APPINSIGHTS_INSTRUMENTATIONKEY. 如果你的函数应用由于某个原因未设置检测密钥,则你需要启用 Application Insights 集成If for some reason your function app doesn't have the instrumentation key set, you need to enable Application Insights integration.

收集遥测数据Collecting telemetry data

启用 Application Insights 集成后,遥测数据将发送到所连接的 Application Insights 实例。With Application Insights integration enabled, telemetry data is sent to your connected Application Insights instance. 此数据包括由 Functions 主机生成的日志、从你的函数代码写入的跟踪,以及性能数据。This data includes logs generated by the Functions host, traces written from your functions code, and performance data.


除了来自你的函数和 Functions 主机的数据之外,你还可以收集来自 Functions 缩放控制器的数据。In addition to data from your functions and the Functions host, you can also collect data from the Functions scale controller.

日志级别和类别Log levels and categories

从应用程序代码写入跟踪时,你应当为跟踪分配一个日志级别。When you write traces from your application code, you should assign a log level to the traces. 日志级别为你提供了一种限制从跟踪中收集的数据量的方法。Log levels provide a way for you to limit the amount of data that is collected from your traces.

为每个日志分配日志级别。A log level is assigned to every log. 该值是表示相对重要性的整数:The value is an integer that indicates relative importance:

LogLevelLogLevel 代码Code 说明Description
跟踪Trace 00 包含最详细消息的日志。Logs that contain the most detailed messages. 这些消息可能包含敏感应用程序数据。These messages may contain sensitive application data. 这些消息默认情况下处于禁用状态,并且绝不应在生产环境中启用。These messages are disabled by default and should never be enabled in a production environment.
调试Debug 11 在开发过程中用于交互式调查的日志。Logs that are used for interactive investigation during development. 这些日志应主要包含对调试有用的信息,并且没有长期价值。These logs should primarily contain information useful for debugging and have no long-term value.
信息Information 22 跟踪应用程序的常规流的日志。Logs that track the general flow of the application. 这些日志应具有长期价值。These logs should have long-term value.
警告Warning 33 突出显示应用程序流中的异常或意外事件,但不会导致应用程序执行停止的日志。Logs that highlight an abnormal or unexpected event in the application flow, but don't otherwise cause the application execution to stop.
错误Error 44 当前执行流因失败而停止时突出显示的日志。Logs that highlight when the current flow of execution is stopped because of a failure. 这些错误应指示当前活动中的故障,而不是应用程序范围内的故障。These errors should indicate a failure in the current activity, not an application-wide failure.
严重Critical 55 描述不可恢复的应用程序/系统崩溃或需要立即引起注意的灾难性故障的日志。Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention.
None 66 禁用指定类别的日志记录。Disables logging for the specified category.

Host.json 文件配置确定函数应用发送到 Application Insights 的日志记录数量。The host.json file configuration determines how much logging a functions app sends to Application Insights.

若要详细了解日志级别,请参阅配置日志级别To learn more about log levels, see Configure log levels.

通过将所记录的项分配到某个类别,可以更好地在函数应用中控制从特定源生成的遥测数据。By assigning logged items to a category, you have more control over telemetry generated from specific sources in your function app. 使用类别,可以更轻松地对所收集的数据运行分析。Categories make it easier to run analytics over collected data. 从函数代码写入的跟踪将根据函数名称分配给各个类别。Traces written from your function code are assigned to individual categories based on the function name. 若要详细了解类别,请参阅配置类别To learn more about categories, see Configure categories.

自定义遥测数据Custom telemetry data

C#JavaScript 中,可以使用 Application Insights SDK 编写自定义遥测数据。In C# and JavaScript, you can use an Application Insights SDK to write custom telemetry data.


从 Functions 2.x 开始,运行时自动收集使用某些客户端 SDK 的绑定的依赖项数据。Starting with version 2.x of Functions, the runtime automatically collects data on dependencies for bindings that use certain client SDKs. Application Insights 收集以下依赖项的数据:Application Insights collects data on the following dependencies:

  • Azure Cosmos DBAzure Cosmos DB
  • Azure 事件中心Azure Event Hubs
  • Azure 服务总线Azure Service Bus
  • Azure 存储服务(Blob、队列和表)Azure Storage services (Blob, Queue, and Table)

还将捕获使用 SqlClient 的 HTTP 请求和数据库调用。HTTP requests and database calls using SqlClient are also captured. 有关 Application Insights 支持的依赖项的完整列表,请参阅自动跟踪的依赖项For the complete list of dependencies supported by Application Insights, see automatically tracked dependencies.

Application Insights 生成收集的依赖项数据的 应用程序映射Application Insights generates an application map of collected dependency data. 下面是包含队列存储输出绑定的 HTTP 触发器函数的应用程序映射示例。The following is an example of an application map of an HTTP trigger function with a Queue storage output binding.


依赖项是在 Information 级别写入的。Dependencies are written at the Information level. 如果你在 Warning 或更高级别进行筛选,则不会看到依赖项数据。If you filter at Warning or above, you won't see the dependency data. 此外,自动收集依赖项的操作在非用户范围内进行。Also, automatic collection of dependencies happens at a non-user scope. 若要捕获依赖项数据,请确保在主机中将级别至少设置为用户范围 (Function.<YOUR_FUNCTION_NAME>.User) 外部的 InformationTo capture dependency data, make sure the level is set to at least Information outside the user scope (Function.<YOUR_FUNCTION_NAME>.User) in your host.

除了自动收集依赖项数据外,还可以使用特定于语言的 Application Insights SDK 之一将自定义依赖项信息写入到日志中。In addition to automatic dependency data collection, you can also use one of the language-specific Application Insights SDKs to write custom dependency information to the logs. 有关如何编写自定义依赖项的示例,请参阅以下特定于语言的示例之一:For an example how to write custom dependencies, see one of the following language-specific examples:

写入到日志Writing to logs

写入到日志的方式和使用的 API 取决于函数应用项目的语言。The way that you write to logs and the APIs you use depend on the language of your function app project.
请参阅适用于你的语言的开发人员指南,详细了解如何从函数写入日志。See the developer guide for your language to learn more about writing logs from your functions.

分析数据Analyze data

默认情况下,从函数应用中收集的数据会存储在 Application Insights 中。By default, the data collected from your function app is stored in Application Insights. Azure 门户中,Application Insights 提供了一组全面的遥测数据可视化效果。In the Azure portal, Application Insights provides an extensive set of visualizations of your telemetry data. 你可以深入查看错误日志,并且可以查询事件和指标。You can drill into error logs and query events and metrics. 若要了解详细信息,包括关于如何查看和查询已收集数据的示例,请参阅在 Application Insights 中分析 Azure Functions 遥测数据To learn more, including basic examples of how to view and query your collected data, see Analyze Azure Functions telemetry in Application Insights.

流式处理日志Streaming Logs

开发应用程序时,通常需要了解在 Azure 中运行时近实时地写入日志的内容。While developing an application, you often want to see what's being written to the logs in near real time when running in Azure.

可以通过两种方式查看通过执行函数生成的日志数据的流。There are two ways to view a stream of the log data being generated by your function executions.

  • 内置日志流式处理:借助应用服务平台即可查看应用程序日志文件流。Built-in log streaming: the App Service platform lets you view a stream of your application log files. 此流等同于在本地开发期间调试函数时以及在门户中使用“测试”选项卡时看到的输出。This stream is equivalent to the output seen when you debug your functions during local development and when you use the Test tab in the portal. 此时将显示所有基于日志的信息。All log-based information is displayed. 有关详细信息,请参阅流式处理日志For more information, see Stream logs. 这种流式处理方法仅支持单个实例,不能用于在消耗计划中的 Linux 上运行的应用。This streaming method supports only a single instance, and can't be used with an app running on Linux in a Consumption plan.

  • 实时指标流:当函数应用 连接到 Application Insights 时,你可以在 Azure 门户中使用 实时指标流近乎实时地查看日志数据和其他指标。Live Metrics Stream: when your function app is connected to Application Insights, you can view log data and other metrics in near real time in the Azure portal using Live Metrics Stream. 当监视在多个实例或消耗计划中的 Linux 上运行的函数时,请使用此方法。Use this method when monitoring functions running on multiple-instances or on Linux in a Consumption plan. 此方法使用抽样数据This method uses sampled data.

可以在门户和大多数本地开发环境中查看日志流。Log streams can be viewed both in the portal and in most local development environments. 若要了解如何启用日志流,请参阅在 Azure Functions 中启用流式处理执行日志To learn how to enable log streams, see Enable streaming execution logs in Azure Functions.

诊断日志Diagnostic logs

此功能为预览版。This feature is in preview.

Application Insights 允许你将遥测数据导出到长期存储或其他分析服务。Application Insights lets you export telemetry data to long-term storage or other analysis services.

由于 Functions 还与 Azure Monitor 集成,因此你还可以使用诊断设置将遥测数据发送到各种目标(包括 Azure Monitor 日志)。Because Functions also integrates with Azure Monitor, you can also use diagnostic settings to send telemetry data to various destinations, including Azure Monitor logs.

缩放控制器日志Scale controller logs

此功能为预览版。This feature is in preview.

Azure Functions 缩放控制器监视运行应用的 Azure Functions 主机的实例。The Azure Functions scale controller monitors instances of the Azure Functions host on which your app runs. 此控制器根据当前性能决定何时添加或删除实例。This controller makes decisions about when to add or remove instances based on current performance. 可以让缩放控制器将日志发出到 Application Insights,以便更好地了解缩放控制器为函数应用做出的决策。You can have the scale controller emit logs to Application Insights to better understand the decisions the scale controller is making for your function app. 你还可以将生成的日志存储在 Blob 存储中,供其他服务分析。You can also store the generated logs in Blob storage for analysis by another service.

若要启用此功能,请将名为 SCALE_CONTROLLER_LOGGING_ENABLED 的应用程序设置添加到函数应用设置中。To enable this feature, you add an application setting named SCALE_CONTROLLER_LOGGING_ENABLED to your function app settings. 若要了解详细方法,请参阅配置缩放控制器日志To learn how, see Configure scale controller logs.

报告问题Report issues

若要报告 Functions 中的 Application Insights 集成问题,或提出建议或请求,请在 GitHub 中创建问题To report an issue with Application Insights integration in Functions, or to make a suggestion or request, create an issue in GitHub.

后续步骤Next steps

有关详细信息,请参阅以下资源:For more information, see the following resources: