Logging, metrics, and telemetry in Azure
There are several options for logging, metrics, and telemetry when using Azure. Review the options to find the tool or service you are looking for:
- Azure Resource metrics - when you use Azure services, Azure monitors your individual resources and collects metrics.
- Custom logging - when your application (on-prem, cloud, or hybrid), needs to log information.
Azure Monitor maximizes the availability and performance of your applications and services by delivering a comprehensive solution for collecting, analyzing, and acting on telemetry from your cloud and on-premises environments.
Turn on Azure resource monitoring in the Azure portal
Enable Application Insights for your resource. This integration is usually available at resource creation time and after the resource is created. The process creates a separate Application Insights resource for logging.
View web app metrics data
View metrics for your resource on a scheduled basis in the Azure portal for each resource.
View web app failure data
View failures for Application Insights monitored resources.
Set alerts to monitor your resource
Set alerts for your resource in the Azure portal for each resource. Alerts can include specific metrics, communication streams (such as email), and frequency. Common alerts to set are total:
- Requests
- Response time
- Http server errors (in Hosting environments)
Custom logging to Azure
Custom logging is automatically provided by Azure web apps and Azure functions, if you use the correct logging functions:
- Web apps use
console.log('your message here') - Function apps use
context.log('your message here')
You can add richer custom logging with Azure Monitor's Application Insights, which offers Server (Node.js) and Client (browser) scenarios:
- Add Application Insights SDK to your source code.
- Server - log from Node.js with Application Insights - npm package
- Client - log from your client code - npm package
- Containers and VMs - log from your Kubernetes cluster or Azure Virtual machines
Local development with Application Insights
If you are trying out Application Insights by running code locally, which uses one of the Application Insights npm packages, make sure to call the flush() method so the logging is sent to Application Insights immediately. When you view the logs, remember that it can still take a couple of minutes before your custom logs are available in Application Insights.
Query your custom logs with Kusto query language
When you use the context.log in a Function app or console.log in a Web app, and you have Application Insights enabled, those custom logs are added to your Application Insights resource in the Trace table. If you prefix your custom log with a specific string, such as JavaScript, you can search the Trace table for any messages that contain that prefix when you want to reduce your log to just those custom entries, using the Kusto query language.
traces
| where message contains "JavaScript"
Configure web app log streaming
View log stream of hosted resources available in the resource's Monitoring section of the Azure portal. Configure them with the App service log configuration.
View web app log streaming
For Azure Web apps, use the following table to learn more about how to stream logs:
| Method | Description |
|---|---|
| Azure CLI | az webapp log tail |
| VSCode App service extension | Right-click on resource and select Start streaming logs |
View Function log streaming
For Azure Function apps, use the following table to learn more about how to stream logs:
| Method | Description |
|---|---|
| Azure CLI | az webapp log tail --resource-group <RESOURCE_GROUP_NAME> --name <FUNCTION_APP_NAME> |
| VSCode Functions service extension | Right-click on resource and select Start streaming logs |