How to Monitor Azure App Service: Simple Steps and Examples
To monitor an Azure App Service, use
Azure Monitor and Application Insights to track performance, availability, and errors. Enable diagnostic logs and set alerts to get notified about issues automatically.Syntax
Monitoring an Azure App Service involves enabling and configuring these key components:
- Application Insights: Tracks app performance and errors.
- Diagnostic Logs: Collects detailed logs like HTTP requests and failures.
- Alerts: Notify you when metrics cross thresholds.
Use Azure CLI or Azure Portal to enable these features.
bash
az monitor app-insights component create --app MyAppService --location eastus --resource-group MyResourceGroup az webapp log config --name MyAppService --resource-group MyResourceGroup --application-logging true --detailed-error-messages true --failed-request-tracing true
Example
This example shows how to enable Application Insights and diagnostic logs for an Azure App Service using Azure CLI. It also demonstrates how to create an alert rule for CPU usage.
bash
az monitor app-insights component create --app MyAppService --location eastus --resource-group MyResourceGroup az webapp log config --name MyAppService --resource-group MyResourceGroup --application-logging true --detailed-error-messages true --failed-request-tracing true az monitor metrics alert create --name HighCPUAlert --resource-group MyResourceGroup --scopes /subscriptions/{subscriptionId}/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyAppService --condition "avg Percentage CPU > 80" --description "Alert when CPU usage is over 80%" --action /subscriptions/{subscriptionId}/resourceGroups/MyResourceGroup/providers/microsoft.insights/actionGroups/MyActionGroup
Output
Application Insights component 'MyAppService' created.
Web app logging enabled.
Alert rule 'HighCPUAlert' created.
Common Pitfalls
- Not enabling Application Insights before deploying the app means no telemetry data is collected.
- Forgetting to enable diagnostic logs results in missing detailed error information.
- Setting alert thresholds too high or too low can cause missed issues or too many false alarms.
- Not linking alerts to action groups means you won't get notified.
bash
az webapp log config --name MyAppService --resource-group MyResourceGroup --application-logging false # Correct way: az webapp log config --name MyAppService --resource-group MyResourceGroup --application-logging true
Quick Reference
| Feature | Purpose | How to Enable |
|---|---|---|
| Application Insights | Track app performance and errors | az monitor app-insights component create |
| Diagnostic Logs | Collect detailed logs and errors | az webapp log config --application-logging true |
| Alerts | Notify on metric thresholds | az monitor metrics alert create |
| Action Groups | Define alert notifications | az monitor action-group create |
Key Takeaways
Enable Application Insights to collect app performance and error data.
Turn on diagnostic logs for detailed request and error information.
Create alerts with appropriate thresholds to get notified of issues.
Link alerts to action groups to receive notifications via email or SMS.
Use Azure CLI or Portal to configure monitoring easily.