[Python] Activity log for specific azure resource

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to query the activity logs of a specific azure resource. However, I am not sure how to do it. I only found base code on the internet that can only filter up to resource group level.

    from azure.mgmt.monitor import MonitorManagementClient
    import datetime

    # Get a client for Monitor
    credentials = connectSP() # Custom function to get credentials
    client = MonitorManagementClient(
    credentials,
    sub_id
    )


    # Generate query here
    today = datetime.datetime.now().date()
    filter = "eventTimestamp ge {}".format(today)
    select = ",".join([
    "eventTimestamp",
    "eventName",
    "operationName",
    "resourceGroupName",
    ])


    # Grab activity logs
    activity_logs = client.activity_logs.list(
    filter=filter,
    select=select
    )

    # Print the logs
    for log in activity_logs:
    print(" ".join([
    str(log.event_timestamp),
    str(log.resource_group_name),
    log.event_name.localized_value,
    log.operation_name.localized_value
    ]))



    I tried to filter it by resource_id attribute but is met with this error:

    Code: BadRequest
    Message: The filter property: resource_id is not supported.


    Is it possible to narrow down the scope to just a resource? Also is there any documentation on how to modify the filter query? I just found the basic ones in the Microsoft documentation. https://learn.microsoft.com/en-us/p...ions.activitylogsoperations?view=azure-python

    Continue reading...

Compartilhe esta Página