Configuring Log Analytics Workspaces

Watch the video to deepen your understanding.
SubscribeComplete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
Configuring Log Analytics Workspaces
Introduction: The Central Hub for Azure Monitoring
In the dynamic world of cloud computing, robust logging and monitoring are not just good practices—they are necessities for maintaining performance, security, and operational efficiency. Azure Monitor provides a comprehensive solution for collecting, analyzing, and acting on telemetry from your cloud and on-premises environments. At the heart of Azure Monitor's logging capabilities lies the Log Analytics Workspace.
A Log Analytics Workspace (LAW) acts as a unique data repository where log data from various sources in Azure and beyond is collected, aggregated, and stored. Think of it as the central brain that receives and processes all your operational intelligence. Without a properly configured LAW, you cannot effectively leverage the power of Azure Monitor for deep insights, proactive alerting, and historical analysis.
Why is it essential?
- Centralized Data Collection: Gathers logs from Azure resources, virtual machines, containers, network devices, and custom applications into a single, queryable location.
- Powerful Analytics: Enables deep analysis of log data using the Kusto Query Language (KQL), allowing you to identify trends, diagnose issues, and uncover security threats.
- Proactive Alerting: Powers Azure Monitor alerts, notifying you of critical events or performance thresholds being crossed.
- Visualization and Reporting: Serves as the data source for Azure Dashboards and Workbooks, providing visual insights into your environment.
- Compliance and Auditing: Retains logs for specified periods, helping meet compliance requirements and facilitating security audits.
Understanding Log Analytics Workspaces
Before diving into configuration, let's understand the core components and key properties of a Log Analytics Workspace.
Core Components and Purpose
A LAW is more than just storage; it's an analytical engine. When data is ingested, it's parsed, indexed, and stored in tables, making it highly efficient for complex queries. Each workspace is isolated, meaning data from one workspace cannot be directly queried from another, though cross-workspace queries are possible with specific permissions.
Key Properties
When designing and configuring your Log Analytics Workspace, several properties are crucial for performance, cost, and compliance:
SKU (Pricing Tier): The SKU determines the pricing model for your workspace. Common SKUs include:
- Pay-as-you-go: Standard pricing based on data ingestion and retention.
- Commitment Tiers: Offer discounted prices for committing to a certain level of data ingestion per day. Ideal for predictable, high-volume environments.
- Free: Limited to 500 MB of data ingestion per day with 7-day retention. Primarily for evaluation.
- Deprecated Tiers: Some older tiers like
Standard,Premium,Per GB2018might still exist but new workspaces should use current tiers.
Impact: Directly affects your monthly Azure bill. Choosing the right SKU based on your expected data volume is critical for cost optimization.
Data Retention: This setting defines how long ingested data is kept in the workspace.
- You can configure retention from 30 days up to 730 days (2 years) or even longer for specific tables.
- Longer retention periods incur higher costs.
- Some data types (e.g.,
AzureActivity) have a default 90-day retention at no extra charge, which is independent of the workspace's overall retention setting.
Impact: Crucial for compliance requirements (e.g., GDPR, HIPAA) and for historical analysis.
Daily Data Cap: An optional, but highly recommended, setting that allows you to specify a maximum amount of data (in GB) that can be ingested into your workspace per day.
- Once the cap is reached, data ingestion stops for the remainder of the day and resumes the next day.
- This prevents unexpected cost spikes due to misconfigured resources or malicious attacks generating excessive logs.
Impact: A vital cost control mechanism.
Region: The Azure region where your Log Analytics Workspace is deployed.
- It's generally recommended to deploy your workspace in the same region as the majority of the resources sending data to it to minimize latency and potential data transfer costs.
Impact: Affects latency, data sovereignty, and potentially data egress costs if data is sent across regions.
Creating a Log Analytics Workspace
You can create a Log Analytics Workspace using the Azure Portal, Azure CLI, or ARM Templates for automation.
Azure Portal (Brief Overview)
- Navigate to the Azure Portal.
- Search for "Log Analytics workspaces" and click "Create".
- Fill in basic details: Subscription, Resource Group, Name, Region, and Pricing Tier.
- Click "Review + create", then "Create".
Azure CLI (Code Snippet)
Using Azure CLI is a common way to automate resource deployment.
# Define variables
RESOURCE_GROUP_NAME="my-monitoring-rg"
WORKSPACE_NAME="my-law-prod-eastus"
LOCATION="eastus"
SKU="PerGB2018" # Or "Consumption" for newer tiers, or a Commitment Tier like "100GB_Commitment"
RETENTION_DAYS=90 # 30 to 730 days
# Create a resource group if it doesn't exist
az group create --name $RESOURCE_GROUP_NAME --location $LOCATION
# Create the Log Analytics Workspace
az monitor log-analytics workspace create \
--resource-group $RESOURCE_GROUP_NAME \
--workspace-name $WORKSPACE_NAME \
--location $LOCATION \
--sku $SKU \
--retention-time $RETENTION_DAYS
# Optionally set a daily data cap (e.g., 50 GB)
az monitor log-analytics workspace update \
--resource-group $RESOURCE_GROUP_NAME \
--workspace-name $WORKSPACE_NAME \
--daily-quota-gb 50
ARM Template (Code Snippet)
For infrastructure as code (IaC), ARM templates are ideal.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"workspaceName": {
"type": "string",
"defaultValue": "my-law-prod-westus",
"metadata": {
"description": "Name of the Log Analytics workspace."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for the Log Analytics workspace."
}
},
"sku": {
"type": "string",
"defaultValue": "PerGB2018",
"allowedValues": [
"Free",
"PerGB2018",
"Consumption",
"100GB_Commitment",
"200GB_Commitment",
"300GB_Commitment",
"400GB_Commitment",
"500GB_Commitment",
"1000GB_Commitment",
"2000GB_Commitment",
"5000GB_Commitment"
],
"metadata": {
"description": "Pricing tier for the Log Analytics workspace."
}
},
"retentionInDays": {
"type": "int",
"defaultValue": 90,
"minValue": 30,
"maxValue": 730,
"metadata": {
"description": "Number of days to retain data."
}
},
"dailyQuotaGb": {
"type": "int",
"defaultValue": 50,
"metadata": {
"description": "Daily data ingestion cap in GB."
}
}
},
"resources": [
{
"type": "Microsoft.OperationalInsights/workspaces",
"apiVersion": "2022-10-01",
"name": "[parameters('workspaceName')]",
"location": "[parameters('location')]",
"properties": {
"sku": {
"name": "[parameters('sku')]"
},
"retentionInDays": "[parameters('retentionInDays')]",
"workspaceCapping": {
"dailyQuotaGb": "[parameters('dailyQuotaGb')]"
}
}
}
]
}
Connecting Data Sources
Once your workspace is created, the next step is to populate it with data.
Azure Resources (Diagnostic Settings)
Most Azure services (e.g., App Services, Key Vaults, Network Security Groups, Storage Accounts) can send their diagnostic logs and metrics directly to a Log Analytics Workspace via Diagnostic Settings.
- Navigate to an Azure resource (e.g., an Azure Function App).
- In the left-hand menu, under "Monitoring," select "Diagnostic settings."
- Click "Add diagnostic setting."
- Give it a name.
- Under "Destination details," select "Send to Log Analytics workspace."
- Choose your Subscription and the target Log Analytics Workspace.
- Select the log categories and metrics you want to send.
- Click "Save."
Azure Virtual Machines (Azure Monitor Agent)
For Azure VMs, on-premises servers, and other cloud VMs, the Azure Monitor Agent (AMA) is the primary method for collecting guest-level telemetry. This agent replaces the legacy Log Analytics agent (MMA).
- Navigate to your Log Analytics Workspace.
- Under "Settings," select "Agents and virtual machines."
- Follow the instructions to deploy the Azure Monitor Agent to your VMs. This typically involves creating a Data Collection Rule (DCR) that specifies what data to collect and where to send it (your LAW).
- You can also deploy AMA via Azure Policy, ARM templates, or the Azure Portal's VM blade.
Custom Logs (Brief Mention)
For applications generating custom log files or using specific logging frameworks, you can configure custom log collection or use data ingest APIs to send data directly to your workspace.
Querying Data with Kusto Query Language (KQL)
KQL is a powerful query language used to interact with data in Log Analytics Workspaces. It's designed for efficiency and flexibility.
Basic KQL Syntax
A KQL query typically starts with a table name, followed by operators separated by pipes (|).
TableName | operator1 | operator2 | ...
Practical KQL Examples
Let's explore some common queries:
1. View VM Heartbeat logs:
The Heartbeat table indicates if the Azure Monitor Agent is successfully communicating.
Heartbeat
| where OSType == "Windows" // Filter for Windows VMs
| summarize LastHeartbeat = max(TimeGenerated) by Computer // Get the last heartbeat for each computer
| extend TimeSinceLastHeartbeat = now() - LastHeartbeat // Calculate time since last heartbeat
| where TimeSinceLastHeartbeat > 5m // Find VMs with no heartbeat in the last 5 minutes
2. Analyze Azure Activity Logs:
The AzureActivity table contains logs from Azure subscriptions, providing insights into control-plane operations.
AzureActivity
| where TimeGenerated > ago(1h) // Logs from the last hour
| where ActivityStatus == "Failed" // Find failed operations
| project TimeGenerated, OperationName, ResourceGroup, Caller, CorrelationId // Select relevant columns
| sort by TimeGenerated desc
3. Monitor VM Performance (CPU Usage):
The Perf table stores performance counters from VMs.
Perf
| where ObjectName == "Processor" and CounterName == "% Processor Time" // CPU usage
| summarize avg(CounterValue) by Computer, bin(TimeGenerated, 1h) // Average CPU per hour per computer
| render timechart // Visualize as a time chart
4. Query App Service Logs:
If you send App Service logs to LAW, they typically land in AppServiceLogs.
AppServiceLogs
| where LogLevel == "Error" // Filter for error logs
| project TimeGenerated, AppName, Message // Show time, app name, and error message
| sort by TimeGenerated desc
Access Control and Security
Securing your Log Analytics Workspace is paramount. Azure Role-Based Access Control (RBAC) is used to manage who can access and perform operations within your workspace.
- Workspace-level permissions: Grant roles like "Log Analytics Reader," "Log Analytics Contributor," or "Owner" to control access to the entire workspace.
- Table-level permissions: For
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons