使用 Azure Monitor REST API 检索活动日志数据

Azure 活动日志是一种日志,方便用户了解对订阅中的资源执行的操作。 操作包括对资源执行的创建、更新、删除和其他操作。 活动日志是一个平台范围的日志,不限于特定服务。 本文介绍如何使用 Azure Monitor REST API 检索活动日志数据。 有关活动日志的详细信息,请参阅 Azure 活动日志事件架构

身份验证

若要检索资源日志,必须使用 Microsoft Entra 进行身份验证。 有关详细信息,请参阅 Azure 监控 REST API 演练

检索活动日志数据

使用 Azure Monitor REST API 查询 活动日志 数据。

以下请求格式用于请求活动日志数据。

GET /subscriptions/<subscriptionId>/providers/Microsoft.Insights/eventtypes/management/values \
?api-version=2015-04-01 \
&$filter=<filter> \
&$select=<select>
host: management.chinacloudapi.cn
authorization: Bearer <token>

$过滤器

$filter 可减少收集的数据集。 此参数是必需的,并且至少需要开始日期/时间。 该 $filter 参数接受以下模式:

  • 列出资源组的事件: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceGroupName eq <resourceGroupName>
  • 列出特定资源的事件:$filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceUri eq <resourceURI>
  • 列出时间范围内订阅的事件:$filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z'
  • 列出资源提供程序的事件:$filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and resourceProvider eq <resourceProviderName>
  • 列出相关 ID: $filter=eventTimestamp ge '2014-07-16T04:36:37.6407898Z' and eventTimestamp le '2014-07-20T04:36:37.6407898Z' and correlationId eq '<correlationID> 的事件。

$select

$select 提取返回的事件的指定属性列表。 $select 参数是一个用逗号分隔的属性名称列表,这些名称将被返回。 有效值为:authorizationclaimscorrelationIddescriptioneventDataIdeventNameeventTimestamphttpRequestleveloperationIdoperationNamepropertiesresourceGroupNameresourceProviderNameresourceIdstatussubmissionTimestampsubStatussubscriptionId

以下示例请求使用 Azure Monitor REST API 查询活动日志。

使用筛选器获取活动日志:

以下示例获取资源组MSSupportGroup在日期2023-03-21T20:00:00Z2023-03-24T20:00:00Z之间的活动日志。

GET https://management.chinacloudapi.cn/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&$filter=eventTimestamp ge '2023-03-21T20:00:00Z' and eventTimestamp le '2023-03-24T20:00:00Z' and resourceGroupName eq 'MSSupportGroup'

获取包含筛选器的活动日志,然后选择:

以下示例获取资源组 MSSupportGroup 的活动日志,在日期 2023-03-21T20:00:00Z2023-03-24T20:00:00Z 之间,并返回元素 eventName、operationName、status、eventTimestamp、correlationId、submissionTimestamp 和 level。

GET https://management.chinacloudapi.cn/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&$filter=eventTimestamp ge '2023-03-21T20:00:00Z' and eventTimestamp le '2023-03-24T20:00:00Z'and resourceGroupName eq 'MSSupportGroup'&$select=eventName,operationName,status,eventTimestamp,correlationId,submissionTimestamp,level

后续步骤

流式传输 Azure Monitor 活动日志数据Azure 活动日志事件架构