通过诊断设置分析日志和指标Analyze logs and metrics with diagnostics settings

使用 Azure Spring Cloud 的诊断功能,可以通过以下任何服务分析日志和指标:Using the diagnostics functionality of Azure Spring Cloud, you can analyze logs and metrics with any of the following services:

  • 使用 Azure Log Analytics,其中数据将写入 Azure 存储。Use Azure Log Analytics, where the data is written to Azure Storage. 将日志导出到 Log Analytics 时存在延迟。There is a delay when exporting logs to Log Analytics.
  • 将日志保存到存储帐户进行审核或手动检查。Save logs to a storage account for auditing or manual inspection. 可以指定保留时间(天)。You can specify the retention time (in days).
  • 将日志流式传输到事件中心,供第三方服务或自定义分析解决方案引入。Stream logs to your event hub for ingestion by a third-party service or custom analytics solution.

选择要监视的日志类别和指标类别。Choose the log category and metric category you want to monitor.

提示

只想要流式传输日志?Just want to stream your logs? 看看这个 Azure CLI 命令吧!Check out this Azure CLI command!

日志Logs

日志Log 说明Description
ApplicationConsoleApplicationConsole 所有客户应用程序的控制台日志。Console log of all customer applications.
SystemLogsSystemLogs 当前,只有 Spring Cloud Config Server 记录在此类别中。Currently, only Spring Cloud Config Server logs in this category.

指标Metrics

有关指标的完整列表,请参阅 Spring Cloud 指标For a complete list of metrics, see Spring Cloud Metrics.

若要开始,请启用其中一项服务来接收数据。To get started, enable one of these services to receive the data. 若要了解如何配置 Log Analytics,请参阅开始在 Azure Monitor 中使用 Log AnalyticsTo learn about configuring Log Analytics, see Get started with Log Analytics in Azure Monitor.

配置诊断设置Configure diagnostics settings

  1. 在 Azure 门户中,请转到 Azure Spring Cloud 实例。In the Azure portal, go to your Azure Spring Cloud instance.

  2. 选择“诊断设置”选项,然后选择“添加诊断设置” 。Select diagnostics settings option, and then select Add diagnostics setting.

  3. 输入设置的名称,然后选择要将日志发送到的位置。Enter a name for the setting, and then choose where you want to send the logs. 可选择以下三个选项的任意组合:You can select any combination of the following three options:

    • 存档到存储帐户Archive to a storage account
    • 流式传输到事件中心Stream to an event hub
    • 发送到 Log AnalyticsSend to Log Analytics
  4. 选择要监视的日志类别和指标类别,并指定保留时间(天)。Choose which log category and metric category you want to monitor, and then specify the retention time (in days). 保留时间仅适用于存储帐户。The retention time applies only to the storage account.

  5. 选择“保存”。Select Save.

备注

  1. 发出日志或指标后,可能需要最多 15 分钟它们才会显示在存储帐户、事件中心或 Log Analytics 中。There might be a gap of up to 15 minutes between when logs or metrics are emitted and when they appear in your storage account, your event hub, or Log Analytics.
  2. 如果删除或移动了 Azure Spring Cloud 实例,则该操作将不会级联到诊断设置资源。If the Azure Spring Cloud instance is deleted or moved, the operation will not cascade to the diagnostics settings resources. 必须手动删除诊断设置资源,才能对其父级(即 Azure Spring Cloud 实例)进行操作。The diagnostics settings resources have to be deleted manually before the operation against its parent, i.e. the Azure Spring Cloud instance. 否则,如果为新的 Azure Spring Cloud 实例预配了与已删除实例相同的资源 ID,或者将 Azure Spring Cloud 实例移回,则先前的诊断设置资源将继续对其进行扩展。Otherwise, if a new Azure Spring Cloud instance is provisioned with the same resource ID as the deleted one, or if the Azure Spring Cloud instance is moved back, the previous diagnostics settings resources continue extending it.

查看日志和指标View the logs and metrics

有多种方法可以查看日志和指标,如以下标题中所述。There are various methods to view logs and metrics as described under the following headings.

使用“日志”边栏选项卡Use the Logs blade

  1. 在 Azure 门户中,请转到 Azure Spring Cloud 实例。In the Azure portal, go to your Azure Spring Cloud instance.

  2. 要打开“日志搜索”窗格,请选择“日志” 。To open the Log Search pane, select Logs.

  3. 在“表”搜索框中In the Tables search box

    • 若要查看日志,请输入一个简单的查询,例如:To view logs, enter a simple query such as:
    AppPlatformLogsforSpring
    | limit 50
    
    • 若要查看指标,请输入一个简单的查询,例如:To view metrics, enter a simple query such as:
    AzureMetrics
    | limit 50
    
  4. 若要查看搜索结果,请选择“运行”。To view the search result, select Run.

使用 Log AnalyticsUse Log Analytics

  1. 在 Azure 门户的左窗格中选择“Log Analytics”。In the Azure portal, in the left pane, select Log Analytics.

  2. 选择添加诊断设置时选择的 Log Analytics 工作区。Select the Log Analytics workspace that you chose when you added your diagnostics settings.

  3. 要打开“日志搜索”窗格,请选择“日志” 。To open the Log Search pane, select Logs.

  4. 在“表”搜索框中,In the Tables search box,

    • 若要查看日志,请输入一个简单的查询,例如:to view logs, enter a simple query such as:
    AppPlatformLogsforSpring
    | limit 50
    
    • 若要查看指标,请输入一个简单的查询,例如:to view metrics, enter a simple query such as:
    AzureMetrics
    | limit 50
    
  5. 若要查看搜索结果,请选择“运行”。To view the search result, select Run.

  6. 可以通过设置筛选条件来搜索特定应用程序或实例的日志:You can search the logs of the specific application or instance by setting a filter condition:

    AppPlatformLogsforSpring
    | where ServiceName == "YourServiceName" and AppName == "YourAppName" and InstanceName == "YourInstanceName"
    | limit 50
    

备注

== 区分大小写,但 =~ 不区分大小写。== is case sensitive, but =~ is not.

若要详细了解 Log Analytics 中使用的查询语言,请参阅 Azure Monitor 日志查询To learn more about the query language that's used in Log Analytics, see Azure Monitor log queries. 要从一个集中的客户端查询所有 Log Analytics 日志,请查看 Azure 数据资源管理器To query all your Log Analytics logs from a centralized client, check out Azure Data Explorer.

使用存储帐户Use your storage account

  1. 在 Azure 门户中,在左侧导航面板或搜索框中查找“存储帐户”。In the Azure portal, find Storage accounts in left navigation panel or search box.
  2. 选择添加诊断设置时选择的存储帐户。Select the storage account that you chose when you added your diagnostics settings.
  3. 若要打开“Blob 容器”窗格,请选择“Blob” 。To open the Blob Container pane, select Blobs.
  4. 若要查看应用程序日志,请搜索名为 insights-logs-applicationconsole 的容器。To review application logs, search for a container called insights-logs-applicationconsole.
  5. 若要查看应用程序指标,请搜索名为 insights-metrics-pt1m 的容器。To review application metrics, search for a container called insights-metrics-pt1m.

若要了解有关将诊断信息发送到存储帐户的详细信息,请参阅在 Azure 存储中存储和查看诊断数据To learn more about sending diagnostics information to a storage account, see Store and view diagnostics data in Azure Storage.

使用事件中心Use your event hub

  1. 在 Azure 门户中,在左侧导航面板或搜索框中查找“事件中心”。In the Azure portal, find Event Hubs in left navigation panel or search box.

  2. 搜索并选择添加诊断设置时选择的事件中心。Search for and select the event hub that you chose when you added your diagnostics settings.

  3. 若要打开“事件中心列表”窗格,请选择“事件中心” 。To open the Event Hub List pane, select Event Hubs.

  4. 若要查看应用程序日志,请搜索名为 insights-logs-applicationconsole 的事件中心。To review application logs, search for an event hub called insights-logs-applicationconsole.

  5. 若要查看应用程序指标,请搜索名为 insights-metrics-pt1m 的事件中心。To review application metrics, search for an event hub called insights-metrics-pt1m.

若要了解有关将诊断信息发送到事件中心的详细信息,请参阅使用事件中心流式处理热路径中的 Azure 诊断数据To learn more about sending diagnostics information to an event hub, see Streaming Azure Diagnostics data in the hot path by using Event Hubs.

分析日志Analyze the logs

Azure Log Analytics 与 Kusto 引擎一起运行,因此,你可以查询日志以进行分析。Azure Log Analytics is running with a Kusto engine so you can query your logs for analysis. 有关使用 Kusto 查询日志的快速简介,请查看 Log Analytics 教程For a quick introduction to querying logs by using Kusto, review the Log Analytics tutorial.

应用程序日志提供有关应用程序的运行状况、性能等的关键信息和详细日志。Application logs provide critical information and verbose logs about your application's health, performance, and more. 下一节将介绍一些简单的查询,帮助你理解应用程序的当前和过去状态。In the next sections are some simple queries to help you understand your application's current and past states.

显示 Azure Spring Cloud 中的应用程序日志Show application logs from Azure Spring Cloud

若要查看中 Azure Spring Cloud 的应用程序日志列表(按时间排序,首先显示最新日志),请运行以下查询:To review a list of application logs from Azure Spring Cloud, sorted by time with the most recent logs shown first, run the following query:

AppPlatformLogsforSpring
| project TimeGenerated , ServiceName , AppName , InstanceName , Log
| sort by TimeGenerated desc

显示包含错误或异常的日志条目Show logs entries containing errors or exceptions

若要查看提及错误或异常的未排序日志条目,请运行以下查询:To review unsorted log entries that mention an error or exception, run the following query:

AppPlatformLogsforSpring
| project TimeGenerated , ServiceName , AppName , InstanceName , Log
| where Log contains "error" or Log contains "exception"

使用此查询查找错误,或修改查询词以查找特定的错误代码或异常。Use this query to find errors, or modify the query terms to find specific error codes or exceptions.

显示应用程序在过去一小时内报告的错误和异常数Show the number of errors and exceptions reported by your application over the last hour

若要创建显示应用程序在过去一小时记录的错误和异常数的饼图,请运行以下查询:To create a pie chart that displays the number of errors and exceptions logged by your application in the last hour, run the following query:

AppPlatformLogsforSpring
| where TimeGenerated > ago(1h)
| where Log contains "error" or Log contains "exception"
| summarize count_per_app = count() by AppName
| sort by count_per_app desc
| render piechart

详细了解查询应用程序日志Learn more about querying application logs

Azure Monitor 通过使用 Log Analytics 为查询应用程序日志提供了广泛的支持。Azure Monitor provides extensive support for querying application logs by using Log Analytics. 若要了解有关此服务的详细信息,请参阅 Azure Monitor 中的日志查询入门To learn more about this service, see Get started with log queries in Azure Monitor. 若要详细了解如何生成查询以分析应用程序日志,请参阅 Azure Monitor 中的日志查询概述For more information about building queries to analyze your application logs, see Overview of log queries in Azure Monitor.

常见问题 (FAQ)Frequently asked questions (FAQ)

如何将多行 Java 堆栈跟踪转换为单行 Java 堆栈跟踪?How to convert multi-line Java stack traces into a single line?

有一种变通方法可以将多行堆栈跟踪转换为单行堆栈跟踪。There is a workaround to convert your multi-line stack traces into a single line. 可以修改 Java 日志输出,重新设置堆栈跟踪消息格式,从而将换行符替换为标记。You can modify the Java log output to reformat stack trace messages, replacing newline characters with a token. 如果使用 Java Logback 库,可以通过添加 %replace(%ex){'[\r\n]+', '\\n'}%nopex 来重新设置堆栈跟踪消息格式,如下所示:If you use Java Logback library, you can reformat stack trace messages by adding %replace(%ex){'[\r\n]+', '\\n'}%nopex as follows:

<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                level: %level, message: "%logger{36}: %msg", exceptions: "%replace(%ex){'[\r\n]+', '\\n'}%nopex"%n
            </pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>

然后可以在 Log Analytics 中再次将换行符替换为标记,如下所示:And then you can replace the token with newline characters again in Log Analytics as below:

AppPlatformLogsforSpring
| extend Log = array_strcat(split(Log, '\\n'), '\n')

可以对其他 Java 日志库使用相同的策略。You may be able to use the same strategy for other Java log libraries.

后续步骤Next steps