您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

监视 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?.

当 Azure Functions 中内置 Application Insights 检测时,需要使用有效的检测密钥将函数应用连接到 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 中创建 function app 资源时,会将检测密钥添加到应用程序设置中。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. 此数据包括函数主机生成的日志、从函数代码写入的跟踪和性能数据。This data includes logs generated by the Functions host, traces written from your functions code, and performance data.


除了函数和函数宿主的数据以外,还可以从 函数缩放控制器收集数据。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.js* 确定函数应用发送到 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.


从版本1.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)

还会捕获使用的 HTTP 请求和数据库调用 SqlClientHTTP 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.


依赖项在级别上写入 InformationDependencies 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. 若要捕获依赖关系数据,请确保将级别设置为至少在 Information 用户范围外 (Function.<YOUR_FUNCTION_NAME>.User 主机) 。To 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 取决于 function app 项目的语言。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.

流式处理日志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 允许你将遥测数据导出到长期存储或其他 analysis services。Application Insights lets you export telemetry data to long-term storage or other analysis services.

由于函数还与 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. 若要了解更多信息,请参阅使用 Azure Monitor 日志监视 Azure FunctionsTo learn more, see Monitoring Azure Functions with 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,以便更好地了解缩放控制器为 function app 做出的决策。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 到 function app 设置。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: