在 Azure Functions 中启用流式传输执行日志功能Enable streaming execution logs in Azure Functions

开发应用程序时,通常需要了解在 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 log files 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 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.

门户Portal

可以在门户中查看这两种类型的日志流。You can view both types of log streams in the portal.

内置日志流式处理Built-in log streaming

若要在门户中查看流式处理日志,请在“监视”下选择“日志流式处理”。To view streaming logs in the portal, choose Log streaming under Monitoring .

实时指标流Live Metrics Stream

若要查看应用的实时指标流,请选择函数应用的“概述”选项卡。To view the Live Metrics Stream for your app, select the Overview tab of your function app. Application Insights 启用后,“配置的功能”下将显示“Application Insights”链接 。When you have Application Insights enables, you see an Application Insights link under Configured features . 使用此链接将转到应用的“Application Insights”页。This link takes you to the Application Insights page for your app.

在 Application Insights 中,选择“实时指标流”。In Application Insights, select Live Metrics Stream . “示例遥测”下降显示采样日志条目Sampled log entries are displayed under Sample Telemetry .

在门户中查看实时指标流

Visual Studio CodeVisual Studio Code

若要在 Azure 中为函数应用启用流式处理日志,请执行以下操作:To turn on the streaming logs for your function app in Azure:

  1. 选择 F1 打开命令面板,然后搜索并运行命令“Azure Functions: 开始流式处理日志”。Select F1 to open the command palette, and then search for and run the command Azure Functions: Start Streaming Logs.

  2. 在 Azure 中选择函数应用,然后选择“是” 为函数应用启用应用程序日志记录。Select your function app in Azure, and then select Yes to enable application logging for the function app.

  3. 在 Azure 中触发函数。Trigger your functions in Azure. 请注意,日志数据显示在 Visual Studio Code 的“输出”窗口中。Notice that log data is displayed in the Output window in Visual Studio Code.

  4. 完成后,请记得运行命令“Azure Functions: 停止流式处理日志”为函数应用禁用日志记录。When you're done, remember to run the command Azure Functions: Stop Streaming Logs to disable logging for the function app.

Core ToolsCore Tools

内置日志流式处理Built-in log streaming

使用 logstream 选项开始接收在 Azure 中运行的特定函数应用的流式处理日志,如以下示例所示:Use the logstream option to start receiving streaming logs of a specific function app running in Azure, as in the following example:

func azure functionapp logstream <FunctionAppName>

实时指标流Live Metrics Stream

通过包含 --browser 选项,可在新的浏览器窗口中查看函数应用的实时指标流,如下例所示:You can view the Live Metrics Stream for your function app in a new browser window by including the --browser option, as in the following example:

func azure functionapp logstream <FunctionAppName> --browser

Azure CLIAzure CLI

可以使用 Azure CLI 启用流式处理日志。You can enable streaming logs by using the Azure CLI. 使用以下命令登录,选择订阅并流式传输日志文件:Use the following commands to sign in, choose your subscription, and stream log files:

az login
az account list
az account set --subscription <subscriptionNameOrId>
az webapp log tail --resource-group <RESOURCE_GROUP_NAME> --name <FUNCTION_APP_NAME>

Azure PowerShellAzure PowerShell

可以使用 Azure PowerShell 启用流式处理日志。You can enable streaming logs by using Azure PowerShell. 对于 PowerShell,请使用 Set-AzWebApp 命令在函数应用上启用日志记录,如以下代码片段所示:For PowerShell, use the Set-AzWebApp command to enable logging on the function app, as shown in the following snippet:

# Enable Logs
Set-AzWebApp -RequestTracingEnabled $True -HttpLoggingEnabled $True -DetailedErrorLoggingEnabled $True -ResourceGroupName $ResourceGroupName -Name $AppName

有关详细信息,请参阅完整代码示例For more information, see the complete code example.

后续步骤Next steps