为 Azure 认知服务启用诊断日志记录Enable diagnostic logging for Azure Cognitive Services

本指南逐步说明如何为 Azure 认知服务启用诊断日志记录。This guide provides step-by-step instructions to enable diagnostic logging for an Azure Cognitive Service. 这些日志提供频繁生成的有关资源操作的丰富数据用于识别问题和调试。These logs provide rich, frequent data about the operation of a resource that are used for issue identification and debugging. 在继续之前,你必须拥有至少订阅了一个认知服务(例如语音服务LUIS)的 Azure 帐户。Before you continue, you must have an Azure account with a subscription to at least one Cognitive Service, such as Speech Services, or LUIS.


若要启用诊断日志记录,需要指定某个位置用于存储日志数据。To enable diagnostic logging, you'll need somewhere to store your log data. 本教程使用 Azure 存储和 Log Analytics。This tutorial uses Azure Storage and Log Analytics.

  • Azure 存储 - 保留策略审核、静态分析或备份的诊断日志。Azure storage - Retains diagnostic logs for policy audit, static analysis, or backup. 只要配置设置的用户同时拥有两个订阅的相应 RBAC 访问权限,存储帐户就不必位于发出日志的资源所在的订阅中。The storage account does not have to be in the same subscription as the resource emitting logs as long as the user who configures the setting has appropriate RBAC access to both subscriptions.
  • Log Analytics - 灵活的日志搜索和分析工具,可用于分析 Azure 资源生成的原始日志。Log Analytics - A flexible log search and analytics tool that allows for analysis of raw logs generated by an Azure resource.


还有其他配置选项可供使用。Additional configuration options are available. 有关详细信息,请参阅从 Azure 资源收集和使用日志数据To learn more, see Collect and consume log data from your Azure resources.

启用诊断日志收集Enable diagnostic log collection

首先,让我们使用 Azure 门户启用诊断日志记录。Let's start by enabling diagnostic logging using the Azure portal.


若要使用 PowerShell 或 Azure CLI 启用此功能,请参考从 Azure 资源收集和使用日志数据中提供的说明。To enable this feature using PowerShell or the Azure CLI, use the instructions provided in Collect and consume log data from your Azure resources.

  1. 导航到 Azure 门户。Navigate to the Azure portal. 然后找到并选择某个认知服务资源。Then locate and select a Cognitive Services resource. 例如,LUIS 订阅。For example, your subscription to luis.
  2. 接下来,在左侧导航菜单中找到“监视”,然后选择“诊断设置”。 Next, from the left-hand navigation menu, locate Monitoring and select Diagnostic settings. 此屏幕包含以前为此资源创建的所有诊断设置。This screen contains all previously created diagnostic settings for this resource.
  3. 如果你想要使用以前创建的某个资源,现在可以选择它。If there is a previously created resource that you'd like to use, you can select it now. 否则,请选择“+ 添加诊断设置”。 Otherwise, select + Add diagnostic setting.
  4. 输入设置名称。Enter a name for the setting. 依次选择“存档到存储帐户”、“发送到 Log Analytics”。 Then select Archive to a storage account and Send to log Analytics.
  5. 出现配置提示时,请选择你要用来存储诊断日志的存储帐户和 OMS 工作区。When prompted to configure, select the storage account and OMS workspace that you'd like to use to store you diagnostic logs. 注意:如果你没有存储帐户或 OMS 工作区,请按提示创建一个。Note: If you don't have a storage account or OMS workspace, follow the prompts to create one.
  6. 依次选择“审核” 、RequestResponseAllMetricsSelect Audit, RequestResponse, and AllMetrics. 然后设置诊断日志数据的保留期。Then set the retention period for your diagnostic log data. 如果将保留策略设置为零,则会无限期存储该日志类别的事件。If a retention policy is set to zero, events for that log category are stored indefinitely.
  7. 单击“保存” 。Click Save.

最长可能需要在两个小时之后,日志数据才可供查询和分析。It can take up to two hours before logging data is available to query and analyze. 因此,如果当前未显示任何内容,请不要担心。So don't worry if you don't see anything right away.

从 Azure 存储查看和导出诊断数据View and export diagnostic data from Azure Storage

Azure 存储是一个可靠的对象存储解决方案,它已经过优化,适合存储大量的非结构化数据。Azure Storage is a robust object storage solution that is optimized for storing large amounts of unstructured data. 本部分介绍如何在存储帐户中查询 30 天时间范围内的事务总数,并将数据导出到 Excel。In this section, you'll learn to query your storage account for total transactions over a 30-day timeframe and export the data to excel.

  1. 在 Azure 门户中,找到在上一部分创建的 Azure 存储资源。From the Azure portal, locate the Azure Storage resource that you created in the last section.
  2. 在左侧导航菜单中找到“监视”,然后选择“指标”。 From the left-hand navigation menu, locate Monitoring and select Metrics.
  3. 使用提供的下拉列表配置查询。Use the available drop-downs to configure your query. 对于本示例,我们将时间范围设置为“过去 30 天”,并将指标设置为“事务”。 For this example, let's set the time range to Last 30 days and the metric to Transaction.
  4. 查询完成后,你将看到过去 30 天的事务的可视化效果。When the query is complete, you'll see a visualization of transaction over the last 30 days. 若要导出此数据,请使用页面顶部的“导出到 Excel”按钮。 To export this data, use the Export to Excel button located at the top of the page.

详细了解可对 Azure 存储中的诊断数据执行哪些操作。Learn more about what you can do with diagnostic data in Azure Storage.

在 Log Analytics 中查看日志View logs in Log Analytics

请遵照以下说明浏览资源的 Log Analytics 数据。Follow these instructions to explore log analytics data for your resource.

  1. 在 Azure 门户上的左侧导航菜单中,找到并选择“Log Analytics”。 From the Azure portal, locate and select Log Analytics from the left-hand navigation menu.
  2. 找到并选择启用诊断时创建的资源。Locate and select the resource you created when enabling diagnostics.
  3. 在“常规”下,找到并选择“日志”。 Under General, locate and select Logs. 在此页中,可以针对日志运行查询。From this page, you can run queries against your logs.

示例查询Sample queries

下面是可用于浏览日志数据的几个基本 Kusto 查询。Here are a few basic Kusto queries you can use to explore your log data.

针对指定时间段内来自 Azure 认知服务的所有诊断日志运行此查询:Run this query for all diagnostic logs from Azure Cognitive Services for a specified time period:

| where ResourceProvider == "MICROSOFT.COGNITIVESERVICES"

运行此查询可查看最近的 10 条日志:Run this query to see the 10 most recent logs:

| where ResourceProvider == "MICROSOFT.COGNITIVESERVICES"
| take 10

运行此查询可按资源将操作分组:Run this query to group operations by Resource:

| where ResourceProvider == "MICROSOFT.COGNITIVESERVICES" |
summarize count() by Resource

运行此查询可以确定执行某个操作平均花费的时间:Run this query to find the average time it takes to perform an operation:

| where ResourceProvider == "MICROSOFT.COGNITIVESERVICES"
| summarize avg(DurationMs)
by OperationName

运行此查询可以查看不同时间按 OperationName 划分的操作量,以及每隔 10 秒装箱的操作计数。Run this query to view the volume of operations over time split by OperationName with counts binned for every 10s.

| where ResourceProvider == "MICROSOFT.COGNITIVESERVICES"
| summarize count()
by bin(TimeGenerated, 10s), OperationName
| render areachart kind=unstacked

后续步骤Next steps