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

为 Azure 应用服务中的应用启用诊断日志记录Enable diagnostics logging for apps in Azure App Service

概述Overview

Azure 提供内置诊断功能,可帮助调试应用服务应用Azure provides built-in diagnostics to assist with debugging an App Service app. 在本文中,将了解如何启用诊断日志记录并将检测添加到应用程序,以及如何访问由 Azure 记录的信息。In this article, you learn how to enable diagnostic logging and add instrumentation to your application, as well as how to access the information logged by Azure.

本文使用 Azure 门户和 Azure CLI 来处理诊断日志。This article uses the Azure portal and Azure CLI to work with diagnostic logs. 有关通过 Visual Studio 使用诊断日志的信息,请参阅在 Visual Studio 中对 Azure 进行故障排除For information on working with diagnostic logs using Visual Studio, see Troubleshooting Azure in Visual Studio.

备注

除了本文中所述的日志记录功能以外,Azure 监视还提供新的集成式日志记录功能。In addition to the logging instructions in this article, there's new, integrated logging capability with Azure Monitoring. 可以在将日志发送到 Azure Monitor(预览版)部分找到有关此功能的详细信息。You'll find more on this capability in the Send logs to Azure Monitor (preview) section.

类型Type PlatformPlatform 位置Location 描述Description
应用程序日志记录Application logging Windows、LinuxWindows, Linux 应用服务文件系统和/或 Azure 存储 BlobApp Service file system and/or Azure Storage blobs 记录应用程序代码生成的消息。Logs messages generated by your application code. 这些消息可能由所选的 Web 框架生成,或者由应用程序代码使用你的语言的标准日志记录模式直接生成。The messages can be generated by the web framework you choose, or from your application code directly using the standard logging pattern of your language. 每条消息被分配为以下类别之一:严重错误警告信息调试跟踪Each message is assigned one of the following categories: Critical, Error, Warning, Info, Debug, and Trace. 启用应用程序日志记录时,可以通过设置严重性级别来选择日志记录的详细程度。You can select how verbose you want the logging to be by setting the severity level when you enable application logging.
Web 服务器日志记录Web server logging WindowsWindows 应用服务文件系统或 Azure 存储 BlobApp Service file system or Azure Storage blobs 采用 W3C 扩展日志文件格式的原始 HTTP 请求数据。Raw HTTP request data in the W3C extended log file format. 每条日志消息包含 HTTP 方法、资源 URI、客户端 IP、客户端端口、用户代理、响应代码等数据。Each log message includes data such as the HTTP method, resource URI, client IP, client port, user agent, response code, and so on.
详细错误消息Detailed Error Messages WindowsWindows 应用服务文件系统App Service file system 已发送到客户端浏览器的 .htm 错误页副本。Copies of the .htm error pages that would have been sent to the client browser. 出于安全原因,不应将详细错误页发送到生产环境中的客户端,但每当出现 HTTP 代码为 400 或更高的应用程序错误时,应用服务都可以保存错误页。For security reasons, detailed error pages shouldn't be sent to clients in production, but App Service can save the error page each time an application error occurs that has HTTP code 400 or greater. 该页可能包含有助于确定服务器返回错误代码的原因的信息。The page may contain information that can help determine why the server returns the error code.
失败请求跟踪Failed request tracing WindowsWindows 应用服务文件系统App Service file system 有关失败请求的详细跟踪信息,包括对用于处理请求的 IIS 组件和每个组件所用的时间的跟踪。Detailed tracing information on failed requests, including a trace of the IIS components used to process the request and the time taken in each component. 如果要提高站点性能或隔离特定的 HTTP 错误,这将非常有用。It's useful if you want to improve site performance or isolate a specific HTTP error. 为每个失败的请求生成一个文件夹,其中包含 XML 日志文件,以及用于查看日志文件的 XSL 样式表。One folder is generated for each failed request, which contains the XML log file, and the XSL stylesheet to view the log file with.
部署日志记录Deployment logging Windows、LinuxWindows, Linux 应用服务文件系统App Service file system 有关何时将内容发布到应用的日志。Logs for when you publish content to an app. 部署日志记录会自动发生,它没有可配置的设置。Deployment logging happens automatically and there are no configurable settings for deployment logging. 它可以帮助确定部署失败的原因。It helps you determine why a deployment failed. 例如,如果使用自定义部署脚本,则可以使用部署日志记录来确定脚本失败的原因。For example, if you use a custom deployment script, you might use deployment logging to determine why the script is failing.

备注

应用服务提供了一个专用的交互式诊断工具来帮助你排查应用程序问题。App Service provides a dedicated, interactive diagnostics tool to help you troubleshoot your application. 有关详细信息,请参阅Azure 应用服务诊断概述For more information, see Azure App Service diagnostics overview.

此外,可以使用其他 Azure 服务(例如 Azure Monitor)来改善应用的日志记录和监视功能。In addition, you can use other Azure services to improve the logging and monitoring capabilities of your app, such as Azure Monitor.

启用应用程序日志记录 (Windows)Enable application logging (Windows)

若要在 Azure 门户中为 Windows 应用启用应用程序日志记录,请导航到你的应用,然后选择“应用服务日志”。****To enable application logging for Windows apps in the Azure portal, navigate to your app and select App Service logs.

对“应用程序日志记录(文件系统)”和/或“应用程序日志记录(Blob)”选择“打开”。************Select On for either Application Logging (Filesystem) or Application Logging (Blob), or both.

“文件系统”选项用于临时调试,在 12 小时后会自行关闭。****The Filesystem option is for temporary debugging purposes, and turns itself off in 12 hours. “Blob”选项用于长期日志记录,需要提供一个要将日志写入到的 Blob 存储容器。****The Blob option is for long-term logging, and needs a blob storage container to write logs to. “Blob”选项还会在日志消息中包含其他信息,例如日志消息的来源 VM 实例 ID (InstanceId)、线程 ID (Tid) 和更详细的时间戳 (EventTickCount)。****The Blob option also includes additional information in the log messages, such as the ID of the origin VM instance of the log message (InstanceId), thread ID (Tid), and a more granular timestamp (EventTickCount).

备注

目前,只有 .NET 应用程序日志可以写入到 blob 存储。Currently only .NET application logs can be written to the blob storage. Java、PHP、Node.js、Python 应用程序日志只能存储在应用服务文件系统上(无需修改代码即可将日志写入外部存储)。Java, PHP, Node.js, Python application logs can only be stored on the App Service file system (without code modifications to write logs to external storage).

此外,如果重新生成存储帐户的访问密钥,则必须重置相应的日志记录配置才能使用更新的访问密钥。Also, if you regenerate your storage account's access keys, you must reset the respective logging configuration to use the updated access keys. 为此,请按以下步骤操作:To do this:

  1. 在“配置”**** 选项卡上,将相应的日志记录功能设置为“关闭”****。In the Configure tab, set the respective logging feature to Off. 保存设置。Save your setting.
  2. 再次启用将日志记录到存储帐户 Blob。Enable logging to the storage account blob again. 保存设置。Save your setting.

选择“级别”,即要记录的详细级别。****Select the Level, or the level of details to log. 下表显示了每个级别包含的日志类别:The following table shows the log categories included in each level:

级别Level 包含的类别Included categories
禁用Disabled None
错误Error “错误”、“严重”Error, Critical
警告Warning “警告”、“错误”、“严重”Warning, Error, Critical
信息Information “信息”、“警告”、“错误”、“严重”Info, Warning, Error, Critical
详细Verbose “跟踪”、“调试”、“信息”、“警告”、“错误”、“严重”(所有类别)Trace, Debug, Info, Warning, Error, Critical (all categories)

完成后,选择“保存”。****When finished, select Save.

启用应用程序日志记录(Linux/容器)Enable application logging (Linux/Container)

要在Azure 门户中为 Linux 应用或自定义容器应用启用应用程序日志记录,请导航到应用并选择应用服务日志To enable application logging for Linux apps or custom container apps in the Azure portal, navigate to your app and select App Service logs.

应用程序日志记录中,选择文件系统In Application logging, select File System.

配额 (MB) 中,指定应用程序日志的磁盘配额。In Quota (MB), specify the disk quota for the application logs. 在“保留期(天)”中,设置日志要保留的天数。****In Retention Period (Days), set the number of days the logs should be retained.

完成后,选择“保存”。****When finished, select Save.

启用 Web 服务器日志记录Enable web server logging

若要在 Azure 门户中为 Windows 应用启用 Web 服务器日志记录,请导航到你的应用,然后选择“应用服务日志”。****To enable web server logging for Windows apps in the Azure portal, navigate to your app and select App Service logs.

对于“Web 服务器日志记录”,请选择“存储”以将日志存储在 Blob 存储上,或选择“文件系统”以将日志存储在应用服务文件系统上。************For Web server logging, select Storage to store logs on blob storage, or File System to store logs on the App Service file system.

在“保留期(天)”中,设置日志要保留的天数。****In Retention Period (Days), set the number of days the logs should be retained.

备注

如果重新生成存储帐户的访问密钥,则必须重置相应的日志记录配置才能使用更新的密钥。If you regenerate your storage account's access keys, you must reset the respective logging configuration to use the updated keys. 为此,请按以下步骤操作:To do this:

  1. 在“配置”**** 选项卡上,将相应的日志记录功能设置为“关闭”****。In the Configure tab, set the respective logging feature to Off. 保存设置。Save your setting.
  2. 再次启用将日志记录到存储帐户 Blob。Enable logging to the storage account blob again. 保存设置。Save your setting.

完成后,选择“保存”。****When finished, select Save.

记录详细错误Log detailed errors

若要在 Azure 门户中保存 Windows 应用的错误页或失败请求跟踪,请导航到你的应用,然后选择“应用服务日志”。****To save the error page or failed request tracing for Windows apps in the Azure portal, navigate to your app and select App Service logs.

在“详细错误日志记录”或“失败请求跟踪”下,选择“打开”,然后选择“保存”。****************Under Detailed Error Logging or Failed Request Tracing, select On, then select Save.

这两种类型的日志都将存储在应用服务文件系统中。Both types of logs are stored in the App Service file system. 最多可保留 50 个错误(文件/文件夹)。Up to 50 errors (files/folders) are retained. 当 HTML 文件的数目超过 50 个时,会自动删除最早的 26 个错误。When the number of HTML files exceed 50, the oldest 26 errors are automatically deleted.

在代码中添加日志消息Add log messages in code

在应用程序代码中,可以使用普通的日志记录功能将日志消息发送到应用程序日志。In your application code, you use the usual logging facilities to send log messages to the application logs. 例如:For example:

流式传输日志Stream logs

在实时流式传输日志之前,请启用所需的日志类型。Before you stream logs in real time, enable the log type that you want. 应用服务将会流式传输写入到存储在 /LogFiles 目录 (d:/home/logfiles) 中的、以 .txt、.log 或 .htm 结尾文件的所有信息。Any information written to files ending in .txt, .log, or .htm that are stored in the /LogFiles directory (d:/home/logfiles) is streamed by App Service.

备注

某些类型的日志记录缓冲区会对日志文件执行写入操作,这可能会导致流中的事件变成混乱。Some types of logging buffer write to the log file, which can result in out of order events in the stream. 例如,用户访问页面时出现的应用程序日志项可能显示在该页面请求所对应的 HTTP 日志项的前面。For example, an application log entry that occurs when a user visits a page may be displayed in the stream before the corresponding HTTP log entry for the page request.

在 Azure 门户中配置In Azure portal

若要在 Azure 门户中流式传输日志,请导航到你的应用并选择“日志流”。****To stream logs in the Azure portal, navigate to your app and select Log stream.

在云壳中In Cloud Shell

要在云壳中实时流式传输日志,请使用以下命令:To stream logs live in Cloud Shell, use the following command:

az webapp log tail --name appname --resource-group myResourceGroup

若要筛选特定事件(如错误),请使用 --Filter 参数。To filter specific events, such as errors, use the --Filter parameter. 例如:For example:

az webapp log tail --name appname --resource-group myResourceGroup --filter Error

若要筛选特定日志类型(如 HTTP),请使用 -Path 参数。To filter specific log types, such as HTTP, use the --Path parameter. 例如:For example:

az webapp log tail --name appname --resource-group myResourceGroup --path http

在本地终端中In local terminal

若要在本地控制台中流式传输日志,请安装 Azure CLI登录帐户To stream logs in the local console, install Azure CLI and sign in to your account. 登录后,按照云外壳的说明操作Once signed in, followed the instructions for Cloud Shell

访问日志文件Access log files

如果为日志类型配置 Azure 存储 Blob 选项,需要使用适用于 Azure 存储的客户端工具。If you configure the Azure Storage blobs option for a log type, you need a client tool that works with Azure Storage. 有关详细信息,请参阅 Azure 存储客户端工具For more information, see Azure Storage Client Tools.

对于存储在应用服务文件系统中的日志,最简单的方法是在浏览器中通过以下链接下载 ZIP 文件:For logs stored in the App Service file system, the easiest way is to download the ZIP file in the browser at:

  • Linux/容器应用程序:https://<app-name>.scm.azurewebsites.net/api/logs/docker/zipLinux/container apps: https://<app-name>.scm.azurewebsites.net/api/logs/docker/zip
  • Windows 应用:https://<app-name>.scm.azurewebsites.net/api/dumpWindows apps: https://<app-name>.scm.azurewebsites.net/api/dump

对于 Linux/容器应用,ZIP 文件包含 Docker 主机和 Docker 容器的控制台输出日志。For Linux/container apps, the ZIP file contains console output logs for both the docker host and the docker container. 对于横向扩展应用,ZIP 文件包含每个实例的一组日志。For a scaled-out app, the ZIP file contains one set of logs for each instance. 在应用服务文件系统中,这些日志文件是 /home/LogFiles目录的内容。In the App Service file system, these log files are the contents of the /home/LogFiles directory.

对于 Windows 应用,该 ZIP 文件包含应用服务文件系统中 D:\Home\LogFiles 目录的内容。For Windows apps, the ZIP file contains the contents of the D:\Home\LogFiles directory in the App Service file system. 其结构如下:It has the following structure:

日志类型Log type 目录Directory 描述Description
应用程序日志Application logs /LogFiles/Application//LogFiles/Application/ 包含一个或多个文本文件。Contains one or more text files. 日志消息的格式取决于所用的日志记录提供程序。The format of the log messages depends on the logging provider you use.
失败的请求跟踪Failed Request Traces /LogFiles/W3SVC#########//LogFiles/W3SVC#########/ 包含 XML 文件和一个 XSL 文件。Contains XML files, and an XSL file. 可以在浏览器中查看带格式的 XML 文件。You can view the formatted XML files in the browser.
详细的错误日志Detailed Error Logs /LogFiles/DetailedErrors//LogFiles/DetailedErrors/ 包含 HTM 错误文件。Contains HTM error files. 可以在浏览器中查看 HTM 文件。You can view the HTM files in the browser.
查看失败请求跟踪的另一种方法是在门户中导航到应用页。Another way to view the failed request traces is to navigate to your app page in the portal. 在左侧菜单中选择“诊断和解决问题”,搜索“失败请求跟踪日志”,然后单击相应的图标来浏览和查看所需的跟踪。********From the left menu, select Diagnose and solve problems, then search for Failed Request Tracing Logs, then click the icon to browse and view the trace you want.
Web 服务器日志Web Server Logs /LogFiles/http/RawLogs//LogFiles/http/RawLogs/ 包含使用 W3C 扩展日志文件格式的文本文件。Contains text files formatted using the W3C extended log file format. 可以使用文本编辑器或诸如日志分析程序之类实用工具来阅读此信息。This information can be read using a text editor or a utility like Log Parser.
应用服务不支持 s-computernames-ipcs-version 字段。App Service doesn't support the s-computername, s-ip, or cs-version fields.
部署日志Deployment logs /LogFiles/Git//deployments//LogFiles/Git/ and /deployments/ 包含内部部署进程生成的日志,以及 Git 部署的日志。Contain logs generated by the internal deployment processes, as well as logs for Git deployments.

将日志发送到 Azure Monitor(预览版)Send logs to Azure Monitor (preview)

使用新的 Azure Monitor 集成,可以创建诊断设置(预览版)将日志发送到存储帐户、事件中心和 Log Analytics。With the new Azure Monitor integration, you can create Diagnostic Settings (preview) to send logs to Storage Accounts, Event Hubs and Log Analytics.

诊断设置(预览版)Diagnostic Settings (preview)

支持的日志类型Supported log types

下表显示了支持的日志类型和说明:The following table shows the supported log types and descriptions:

日志类型Log type Windows 支持Windows support Linux(Docker)支持Linux (Docker) support 描述Description
AppServiceConsoleLogsAppServiceConsoleLogs TBATBA Yes 标准输出和标准错误Standard output and standard error
AppServiceHTTPLogsAppServiceHTTPLogs Yes Yes Web 服务器日志Web server logs
AppServiceEnvironmentPlatformLogsAppServiceEnvironmentPlatformLogs Yes Yes 应用服务环境:缩放、配置更改和状态日志App Service Environment: scaling, configuration changes, and status logs
AppServiceAuditLogsAppServiceAuditLogs Yes Yes 通过 FTP 和 Kudu 进行的登录活动Login activity via FTP and Kudu
AppServiceFileAuditLogsAppServiceFileAuditLogs TBATBA Yes 通过 FTP 和 Kudu 进行的文件更改File changes via FTP and Kudu
AppServiceAppLogsAppServiceAppLogs TBATBA Java SE & 汤姆卡特Java SE & Tomcat 应用程序日志Application logs

后续步骤Next steps