Build Lab — Route the Activity Log into a workspace and query it
AZ-305 › Unit 1 › Design solutions for logging and monitoring
Build Lab — Route the Activity Log into a workspace and query it
Build brief
- 1 — Identity, governance, monitoring
- Your own Azure subscription
- 25 minutes
- AzureActivity is free to ingest
- Azure CLI
This is the hands-on twin of "Route the logs once, and only where they earn their keep". There you decided which mechanism moves telemetry. Here you build the mechanism, wait for data to land, and query it — including the part nobody tells you about, which is that logs do not arrive instantly and a working pipeline looks broken for the first few minutes.
Before you start
- Azure CLI, and
az logincompleted. - Contributor on a subscription — you will create one resource group and one workspace.
- Roughly 15 minutes of patience between step 3 and step 5. Use it to read the design lab again.
What you are building
Step 1 — a resource group to hold the workspace
az group create --name bb-lab-logs --location westeuropeStep 2 — create the workspace
az monitor log-analytics workspace create \
--resource-group bb-lab-logs \
--workspace-name bb-lab-workspace \
--location westeuropeKeep the returned id — the next step needs it. You can always fetch it again:
az monitor log-analytics workspace show \
--resource-group bb-lab-logs \
--workspace-name bb-lab-workspace \
--query id -o tsvStep 3 — route the Activity Log at subscription scope
az monitor diagnostic-settings subscription create \
--name bb-lab-activity \
--location westeurope \
--workspace "$(az monitor log-analytics workspace show --resource-group bb-lab-logs --workspace-name bb-lab-workspace --query id -o tsv)" \
--logs '[{"category":"Administrative","enabled":true},{"category":"Policy","enabled":true}]'Two things to notice. This is a subscription-scope diagnostic setting — the Activity Log is a subscription-level source, not a per-resource one, which is why it has its own command. And you named a workspace here; you could have added a storage account and an event hub to the same setting, which is exactly the claim the design lab tested.
Step 4 — generate an event to look for
az group create --name bb-lab-evidence --location westeuropeCreating a resource group is itself an Administrative operation, so this is the event you will hunt for.
Step 5 — wait, then query
Give it 10 to 15 minutes. Then:
az monitor log-analytics query \
--workspace "$(az monitor log-analytics workspace show --resource-group bb-lab-logs --workspace-name bb-lab-workspace --query customerId -o tsv)" \
--analytics-query "AzureActivity | where ResourceGroup =~ 'bb-lab-evidence' | project TimeGenerated, OperationNameValue, Caller | take 10" \
-o tableCheckpoint
You should see at least one row naming a resource-group write operation and your own account as Caller.
If you get zero rows, do not assume it is broken. That is the single most useful thing this lab teaches: ingestion latency is real, and every "my logging is not working" incident starts here. Re-run the query a few minutes later before changing anything. Note the customerId in the query above — the workspace GUID, not its resource id. Using the wrong one is the second most common mistake.
Why this matters on the exam
You routed the Activity Log with a command scoped to the SUBSCRIPTION rather than to a resource. What does that tell you about the Activity Log?
Teardown
az group delete --name bb-lab-evidence --yes --no-wait
az monitor diagnostic-settings subscription delete \
--name bb-lab-activity --yes
az group delete --name bb-lab-logs --yes --no-waitDeleting the resource group removes the workspace with it. Confirm the subscription setting is gone — this one lives outside the resource group and will otherwise keep routing:
az monitor diagnostic-settings subscription list -o tableIf it did not work
Four failures, in the order they usually happen
The query returns nothing
Almost always latency, not configuration. Wait and retry before touching anything. Confirm the pipeline exists with az monitor diagnostic-settings subscription list — if your setting is listed and the workspace id is right, the design is correct and you are early.
Next
Re-read the design lab's compare table — diagnostic setting, data export, cross-workspace query. You have now built the first of the three, and the row that said "a copy crosses the boundary; a query does not" should read very differently now that you have watched a copy take fifteen minutes to arrive.