Querying logs with KQL

AZ-104 Azure AdministratorMonitor and maintainAbout 25 minutesBy Cloud Hands On

Read and write Kusto queries step by step, then practise on sample VM logs in a live playground.

In this lesson you'll learn

1. How KQL reads

New to KQL? Start here

KQL (Kusto Query Language) is how you search logs in Azure. A query starts with a table name, then passes rows through operators joined by the pipe symbol |. Each step takes the rows from the step before, like water through a series of filters.

Rows flow from the Event table through where, summarize and sort, getting fewer at each step Event10,000 rows whereerrors only: 240 summarizeper computer: 6 sort Event | where EventLevelName == "Error" | summarize count() by Computer | sort by count_ desc
Filter early. The fewer rows each step handles, the faster the query.

2. The operators you need

OperatorDoesExample
whereKeeps rows that matchwhere EventLevelName == "Error"
ago()A time relative to nowwhere TimeGenerated > ago(1h)
projectChooses columnsproject Computer, EventID
summarizeGroups and counts or averagessummarize count() by Computer
sort byOrders rowssort by TimeGenerated desc
takeAny N rows, no ordertake 10
topFirst N rows by a columntop 5 by CounterValue desc
searchFull-text searchsearch in (Event) "error"
renderDraws a chartrender timechart

Comparisons: == (exact, case sensitive), =~ (case insensitive), !=, contains, has (whole word, faster), and > < for numbers and times. Combine conditions with and and or.

3. Try it: KQL playground

Two sample tables are loaded: Event (Windows events from three VMs) and Perf (CPU and memory counters). Pick an example or write your own query.

Try it in the app. This lesson has a KQL practice box here. Open the lesson in the exam app

4. Read a query like the exam does

Example: average CPU per computer over the last hour
Perf
| where TimeGenerated > ago(1h)
| where CounterName == "% Processor Time"
| summarize avg(CounterValue) by Computer
| sort by avg_CounterValue desc
  1. Start with the Perf table.
  2. Keep only rows from the last hour.
  3. Keep only the CPU counter.
  4. Group by computer and average the values. The new column is named avg_CounterValue automatically.
  5. Busiest computer first.

Exam focus

Spot wrong answers by syntax. SELECT and FROM are SQL, not KQL. Verb-Noun commands like Get-Event are PowerShell. A KQL answer starts with a table name or with search.

Exam traps to remember

  • take returns random rows. Use top or sort when order matters.
  • summarize count() creates a column named count_.
  • == is case sensitive. Use =~ to ignore case.

Test yourself: 3 check questions and exam-style practice questions on this topic are in the AZ-104 Azure Administrator app.

Practise in the app

Keep going