设置诊断日志记录Setup diagnostic logging

监视服务器性能对于任何 Analysis Services 解决方案都至关重要。An important part of any Analysis Services solution is monitoring how your servers are performing. Azure Analysis Services 与 Azure Monitor 集成。Azure Analysis services is integrated with Azure Monitor. 借助 Azure Monitor 资源日志,可监视日志并将其发送到 Azure 存储,将其流式传输到 Azure 事件中心,并将其导出到 Azure Monitor 日志With Azure Monitor resource logs, you can monitor and send logs to Azure Storage, stream them to Azure Event Hubs, and export them to Azure Monitor logs.

存储、事件中心或 Azure Monitor 日志的资源日志记录

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

会记录哪些内容?What's logged?

可选择“引擎”“服务”和“指标”类别。You can select Engine , Service , and Metrics categories.

引擎Engine

选择“引擎”将记录所有 xEventsSelecting Engine logs all xEvents. 不能选择单个事件。You cannot select individual events.

XEvent 类别XEvent categories 事件名称Event name
安全审核Security Audit 审核登录Audit Login
安全审核Security Audit 审核注销Audit Logout
安全审核Security Audit 审核服务器启动和停止Audit Server Starts And Stops
进度报告Progress Reports 进度报告开始Progress Report Begin
进度报告Progress Reports 进度报告结束Progress Report End
进度报告Progress Reports 进度报告进行中Progress Report Current
查询Queries 查询开始Query Begin
查询Queries 查询结束Query End
命令Commands 命令开始Command Begin
命令Commands 命令结束Command End
错误和警告Errors & Warnings 错误Error
发现Discover 发现结束Discover End
通知Notification 通知Notification
会话Session 会话初始化Session Initialize
Locks 死锁Deadlock
查询处理Query Processing VertiPaq SE 查询开始VertiPaq SE Query Begin
查询处理Query Processing VertiPaq SE 查询结束VertiPaq SE Query End
查询处理Query Processing VertiPaq SE 查询缓存匹配VertiPaq SE Query Cache Match
查询处理Query Processing 直接查询开始Direct Query Begin
查询处理Query Processing 直接查询结束Direct Query End

服务Service

操作名称Operation name 发生条件Occurs when
ResumeServerResumeServer 恢复服务器Resume a server
SuspendServerSuspendServer 暂停服务器Pause a server
DeleteServerDeleteServer 删除服务器Delete a server
RestartServerRestartServer 用户通过 SSMS 或 PowerShell 重启服务器User restarts a server through SSMS or PowerShell
GetServerLogFilesGetServerLogFiles 用户通过 PowerShell 导出服务器日志User exports server log through PowerShell
ExportModelExportModel 用户通过在 Visual Studio 中使用 Open 在门户中导出模型User exports a model in the portal by using Open in Visual Studio

所有指标All metrics

“指标”类别会将相同的服务器指标记录到 AzureMetrics 表中。The Metrics category logs the same Server metrics to the AzureMetrics table. 如果使用的是查询 横向扩展,并且需要为每个只读副本分离指标,请改用 AzureDiagnostics 表(其中 OperationName 等于 LogMetric )。If you're using query scale-out and need to separate metrics for each read replica, use the AzureDiagnostics table instead, where OperationName is equal to LogMetric .

设置诊断日志记录Setup diagnostics logging

Azure 门户Azure portal

  1. Azure 门户中找到服务器,单击左侧导航栏中的“诊断设置”,然后单击“启用诊断” 。In Azure portal > server, click Diagnostic settings in the left navigation, and then click Turn on diagnostics .

    在 Azure 门户中启用 Azure Cosmos DB 的资源日志记录

  2. 在“诊断设置”中,指定以下选项:In Diagnostic settings , specify the following options:

    • NameName . 为要创建的日志输入名称。Enter a name for the logs to create.

    • 存档到存储帐户Archive to a storage account . 要使用此选项,需要一个可连接到的现有存储帐户。To use this option, you need an existing storage account to connect to. 请参阅创建存储帐户See Create a storage account. 按照说明创建一个资源管理器常规用途帐户,然后返回到门户中的此页面来选择存储帐户。Follow the instructions to create a Resource Manager, general-purpose account, then select your storage account by returning to this page in the portal. 新创建的存储帐户可能几分钟后才会显示在下拉菜单中。It may take a few minutes for newly created storage accounts to appear in the drop-down menu.

    • 流式传输到事件中心Stream to an event hub . 要使用此选项,需要一个可连接到的现有事件中心命名空间和事件中心。To use this option, you need an existing Event Hub namespace and event hub to connect to. 若要了解详细信息,请参阅使用 Azure 门户创建事件中心命名空间和事件中心To learn more, see Create an Event Hubs namespace and an event hub using the Azure portal. 然后在门户中返回到此页,选择事件中心命名空间和策略名称。Then return to this page in the portal to select the Event Hub namespace and policy name.

    • 发送到 Log AnalyticsSend to Log Analytics . 若要使用此选项,可以使用现有工作区或者在门户中新建工作区资源。To use this option, either use an existing workspace or create a new workspace resource in the portal. 有关查看日志的详细信息,请参阅本文中的在 Log Analytics 工作区中查看日志For more information on viewing your logs, see View logs in Log Analytics workspace in this article.

      • 引擎Engine . 选择此选项以记录 Xevent。Select this option to log xEvents. 若要存档到存储帐户,可以选择资源日志的保留期。If you're archiving to a storage account, you can select the retention period for the resource logs. 保留期到期后会自动删除日志。Logs are autodeleted after the retention period expires.
      • 服务Service . 选择此选项以记录服务级别事件。Select this option to log service level events. 若要存档到存储帐户,可以选择资源日志的保留期。If you are archiving to a storage account, you can select the retention period for the resource logs. 保留期到期后会自动删除日志。Logs are autodeleted after the retention period expires.
      • 指标Metrics . 选择此选项可在指标中存储详细数据。Select this option to store verbose data in Metrics. 若要存档到存储帐户,可以选择资源日志的保留期。If you are archiving to a storage account, you can select the retention period for the resource logs. 保留期到期后会自动删除日志。Logs are autodeleted after the retention period expires.
  3. 单击“保存” 。Click Save .

    如果收到一个错误,指出“无法更新 <workspace name> 的诊断。If you receive an error that says "Failed to update diagnostics for <workspace name>. 订阅 <subscription id> 未注册,无法使用 microsoft.insights”,The subscription <subscription id> is not registered to use microsoft.insights." 请按 Azure 诊断故障排除说明注册帐户,然后重试此过程。follow the Troubleshoot Azure Diagnostics instructions to register the account, then retry this procedure.

    若要在将来的任意时间点更改资源日志的保存方式,可以返回此页修改设置。If you want to change how your resource logs are saved at any point in the future, you can return to this page to modify settings.

PowerShellPowerShell

以下基本命令可以帮助你开始使用。Here are the basic commands to get you going. 如需有关使用 PowerShell 为存储帐户设置日志记录的逐步指导,请参阅本文稍后部分的教程。If you want step-by-step help on setting up logging to a storage account by using PowerShell, see the tutorial later in this article.

若要使用 PowerShell 启用指标和资源日志记录,请使用以下命令:To enable metrics and resource logging by using PowerShell, use the following commands:

  • 若要允许在存储帐户中存储资源日志,请使用以下命令:To enable storage of resource logs in a storage account, use this command:

    Set-AzDiagnosticSetting -ResourceId [your resource id] -StorageAccountId [your storage account id] -Enabled $true
    

    存储帐户 ID 是需要将日志发送到的存储帐户的资源 ID。The storage account ID is the resource ID for the storage account where you want to send the logs.

  • 若要允许将资源日志流式传输到事件中心,请使用以下命令:To enable streaming of resource logs to an event hub, use this command:

    Set-AzDiagnosticSetting -ResourceId [your resource id] -ServiceBusRuleId [your service bus rule id] -Enabled $true
    

    Azure 服务总线规则 ID 是以下格式的字符串:The Azure Service Bus rule ID is a string with this format:

    {service bus resource ID}/authorizationrules/{key name}
    
  • 若要允许将资源日志发送到 Log Analytics 工作区,请使用以下命令:To enable sending resource logs to a Log Analytics workspace, use this command:

    Set-AzDiagnosticSetting -ResourceId [your resource id] -WorkspaceId [resource id of the log analytics workspace] -Enabled $true
    
  • 可以使用以下命令获取 Log Analytics 工作区的资源 ID:You can obtain the resource ID of your Log Analytics workspace by using the following command:

    (Get-AzOperationalInsightsWorkspace).ResourceId
    

可以结合这些参数启用多个输出选项。You can combine these parameters to enable multiple output options.

REST APIREST API

了解如何使用 Azure Monitor REST API 更改诊断设置Learn how to change diagnostics settings by using the Azure Monitor REST API.

Resource Manager 模板Resource Manager template

了解如何在创建资源时使用资源管理器模板启用诊断设置Learn how to enable diagnostics settings at resource creation by using a Resource Manager template.

管理日志Manage your logs

通常在设置日志记录数小时后可使用日志。Logs are typically available within a couple hours of setting up logging. 存储帐户中的日志完全由你管理:It's up to you to manage your logs in your storage account:

  • 请使用标准的 Azure 访问控制方法限制可访问日志的人员,以此保护日志。Use standard Azure access control methods to secure your logs by restricting who can access them.
  • 删除不想继续保留在存储帐户中的日志。Delete logs that you no longer want to keep in your storage account.
  • 请务必设置保留期,以便从存储帐户中删除旧日志。Be sure to set a retention period for so old logs are deleted from your storage account.

在 Log Analytics 工作区中查看日志View logs in Log Analytics workspace

在 Log Analytics 工作区资源中,指标和服务器事件与 Xevent 集成,以便并列分析。Metrics and server events are integrated with xEvents in your Log Analytics workspace resource for side-by-side analysis. Log Analytics 工作区还可配置为接收来自其他 Azure 服务的事件,从而提供整个体系结构的诊断日志记录数据。Log Analytics workspace can also be configured to receive events from other Azure services providing a holistic view of diagnostic logging data across your architecture.

若要查看诊断数据,请从 Log Analytics 工作区的左侧菜单中打开“日志”。To view your diagnostic data, in Log Analytics workspace, open Logs from the left menu.

Azure 门户中的日志搜索选项

在查询生成器中,展开 LogManagement > AzureDiagnostics 。In the query builder, expand LogManagement > AzureDiagnostics . AzureDiagnostics 包括引擎和服务事件。AzureDiagnostics includes Engine and Service events. 注意即时创建了一个查询。Notice a query is created on-the-fly. EventClass_s 字段包含 xEvent 名称,如果使用 Xevent 进行本地日志记录,你可能觉得该名称很眼熟。The EventClass_s field contains xEvent names, which may look familiar if you've used xEvents for on-premises logging. 单击“EventClass_s”或某个事件名称,Log Analytics 工作区将继续构造查询。Click EventClass_s or one of the event names and Log Analytics workspace continues constructing a query. 请务必保存查询以便稍后重复使用。Be sure to save your queries to reuse later.

查询示例Example queries

示例 1Example 1

以下查询返回模型数据库和服务器的每个查询结束/刷新结束事件的持续时间。The following query returns durations for each query end/refresh end event for a model database and server. 如果进行横向扩展,则结果将按副本细分,因为副本编号包含在 ServerName_s 中。If scaled out, the results are broken out by replica because the replica number is included in ServerName_s. 按 RootActivityId_g 分组可减少从 Azure 诊断 REST API 检索到的行数,并有助于保持在 Log Analytics 速率限制中所述的限制内。Grouping by RootActivityId_g reduces the row count retrieved from the Azure Diagnostics REST API and helps stay within the limits as described in Log Analytics Rate limits.

let window = AzureDiagnostics
   | where ResourceProvider == "MICROSOFT.ANALYSISSERVICES" and Resource =~ "MyServerName" and DatabaseName_s =~ "MyDatabaseName" ;
window
| where OperationName has "QueryEnd" or (OperationName has "CommandEnd" and EventSubclass_s == 38)
| where extract(@"([^,]*)", 1,Duration_s, typeof(long)) > 0
| extend DurationMs=extract(@"([^,]*)", 1,Duration_s, typeof(long))
| project  StartTime_t,EndTime_t,ServerName_s,OperationName,RootActivityId_g,TextData_s,DatabaseName_s,ApplicationName_s,Duration_s,EffectiveUsername_s,User_s,EventSubclass_s,DurationMs
| order by StartTime_t asc

示例 2Example 2

以下查询返回服务器的内存和 QPU 消耗。The following query returns memory and QPU consumption for a server. 如果进行横向扩展,则结果将按副本细分,因为副本编号包含在 ServerName_s 中。If scaled out, the results are broken out by replica because the replica number is included in ServerName_s.

let window = AzureDiagnostics
   | where ResourceProvider == "MICROSOFT.ANALYSISSERVICES" and Resource =~ "MyServerName";
window
| where OperationName == "LogMetric" 
| where name_s == "memory_metric" or name_s == "qpu_metric"
| project ServerName_s, TimeGenerated, name_s, value_s
| summarize avg(todecimal(value_s)) by ServerName_s, name_s, bin(TimeGenerated, 1m)
| order by TimeGenerated asc 

示例 3Example 3

以下查询返回服务器的 Analysis Services 引擎性能计数器每秒读取的行数。The following query returns the Rows read/sec Analysis Services engine performance counters for a server.

let window =  AzureDiagnostics
   | where ResourceProvider == "MICROSOFT.ANALYSISSERVICES" and Resource =~ "MyServerName";
window
| where OperationName == "LogMetric" 
| where parse_json(tostring(parse_json(perfobject_s).counters))[0].name == "Rows read/sec" 
| extend Value = tostring(parse_json(tostring(parse_json(perfobject_s).counters))[0].value) 
| project ServerName_s, TimeGenerated, Value
| summarize avg(todecimal(Value)) by ServerName_s, bin(TimeGenerated, 1m)
| order by TimeGenerated asc 

有成百上千的查询可以使用。There are hundreds of queries you can use. 若要了解有关查询的详细信息,请参阅 Azure Monitor 日志查询入门To learn more about queries, see Get started with Azure Monitor log queries.

使用 PowerShell 启用日志记录Turn on logging by using PowerShell

在此快速教程中,你将在 Analysis Services 服务器所在订阅和资源组中创建存储帐户。In this quick tutorial, you create a storage account in the same subscription and resource group as your Analysis Service server. 然后使用 Set-AzDiagnosticSetting 启用诊断日志记录,将输出发送到新的存储帐户。You then use Set-AzDiagnosticSetting to turn on diagnostics logging, sending output to the new storage account.

先决条件Prerequisites

要完成本教程,必须备好以下资源:To complete this tutorial, you must have the following resources:

连接到订阅Connect to your subscriptions

启动 Azure PowerShell 会话,并使用以下命令登录用户的 Azure 帐户:Start an Azure PowerShell session and sign in to your Azure account with the following command:

Connect-AzAccount -Environment AzureChinaCloud

在弹出的浏览器窗口中,输入 Azure 帐户用户名和密码。In the pop-up browser window, enter your Azure account user name and password. Azure PowerShell 获取与此帐户关联的所有订阅,并按默认使用第一个订阅。Azure PowerShell gets all the subscriptions that are associated with this account and by default, uses the first one.

如果有多个订阅,可能需要指定用来创建 Azure 密钥保管库的特定订阅。If you have multiple subscriptions, you might have to specify a specific one that was used to create your Azure Key Vault. 键入以下命令查看帐户的订阅:Type the following to see the subscriptions for your account:

Get-AzSubscription

然后,键入以下命令,以指定与要记录的 Azure Analysis Services 帐户关联的订阅:Then, to specify the subscription that's associated with the Azure Analysis Services account you are logging, type:

Set-AzContext -SubscriptionId <subscription ID>

备注

如果帐户关联了多个订阅,请务必指定该订阅。If you have multiple subscriptions associated with your account, it is important to specify the subscription.

为日志创建新的存储帐户Create a new storage account for your logs

如果服务器所属订阅中存在现有存储帐户,可对日志使用该帐户。You can use an existing storage account for your logs, provided it's in the same subscription as your server. 对本教程而言,需创建专用于 Analysis Services 日志的新存储帐户。For this tutorial, you create a new storage account dedicated to Analysis Services logs. 为方便起见,将存储帐户详细信息将存储到名为 sa 的变量中。To make it easy, you're storing the storage account details in a variable named sa .

还要使用包含 Analysis Services 服务器的资源组。You also use the same resource group as the one that contains your Analysis Services server. awsales_resgroupawsaleslogsChina North 的值替换为自己的值:Substitute values for awsales_resgroup, awsaleslogs, and China North with your own values:

$sa = New-AzStorageAccount -ResourceGroupName awsales_resgroup `
-Name awsaleslogs -Type Standard_LRS -Location 'China North'

标识用于你的日志的服务器帐户Identify the server account for your logs

将帐户名称设置为名为“account”的变量,其中 ResourceName 是帐户的名称。Set the account name to a variable named account , where ResourceName is the name of the account.

$account = Get-AzResource -ResourceGroupName awsales_resgroup `
-ResourceName awsales -ResourceType "Microsoft.AnalysisServices/servers"

启用日志记录Enable logging

若要启用日志记录,请将 Set-AzDiagnosticSetting cmdlet 与新存储帐户、服务器帐户和类别的变量一起使用。To enable logging, use the Set-AzDiagnosticSetting cmdlet together with the variables for the new storage account, server account, and the category. 运行以下命令,将“-Enabled”标志设置为“$true” :Run the following command, setting the -Enabled flag to $true :

Set-AzDiagnosticSetting  -ResourceId $account.ResourceId -StorageAccountId $sa.Id -Enabled $true -Categories Engine

输出应该类似于以下示例:The output should look something like this example:

StorageAccountId            : 
/subscriptions/a23279b5-xxxx-xxxx-xxxx-47b7c6d423ea/resourceGroups/awsales_resgroup/providers/Microsoft.Storage/storageAccounts/awsaleslogs
ServiceBusRuleId            :
EventHubAuthorizationRuleId :
Metrics                    
    TimeGrain       : PT1M
    Enabled         : False
    RetentionPolicy
    Enabled : False
    Days    : 0

Logs                       
    Category        : Engine
    Enabled         : True
    RetentionPolicy
    Enabled : False
    Days    : 0

    Category        : Service
    Enabled         : False
    RetentionPolicy
    Enabled : False
    Days    : 0

WorkspaceId                 :
Id                          : /subscriptions/a23279b5-xxxx-xxxx-xxxx-47b7c6d423ea/resourcegroups/awsales_resgroup/providers/microsoft.analysisservic
es/servers/awsales/providers/microsoft.insights/diagnosticSettings/service
Name                        : service
Type                        :
Location                    :
Tags                        :

此输出确认已为服务器启用日志记录,会将信息保存到存储帐户。This output confirms that logging is now enabled for the server, saving information to the storage account.

还可为日志设置保留策略,以便自动删除较旧的日志。You can also set retention policy for your logs so older logs are automatically deleted. 例如,使用 -RetentionEnabled 标志将保留期策略设置为 $true ,并将 -RetentionInDays 参数设置为 90For example, set retention policy using -RetentionEnabled flag to $true , and set -RetentionInDays parameter to 90 . 将自动删除超过 90 天的日志。Logs older than 90 days are automatically deleted.

Set-AzDiagnosticSetting -ResourceId $account.ResourceId`
 -StorageAccountId $sa.Id -Enabled $true -Categories Engine`
  -RetentionEnabled $true -RetentionInDays 90

后续步骤Next steps

了解有关 Azure Monitor 资源日志记录的详细信息。Learn more about Azure Monitor resource logging.

请参阅 PowerShell 帮助中的 Set-AzDiagnosticSettingSee Set-AzDiagnosticSetting in PowerShell help.