你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 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.azure.com
authorization: Bearer <token>

$filter

$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 查询活动日志。

使用 filter 命令获取活动日志:

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

GET https://management.azure.com/subscriptions/12345678-abcd-98765432-abcdef012345/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'

使用 filter 和 select 命令获取活动日志:

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

GET https://management.azure.com/subscriptions/12345678-abcd-98765432-abcdef012345/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 活动日志事件架构