title | description | ms.topic | ms.date | ms.custom |
---|---|---|---|---|
JavaScript Logging, metrics, alerts in Azure |
Learn about logging options in Azure |
how-to |
11/28/2022 |
devx-track-js |
In order to understand how your Azure service is performing, you need to understand what logging, metrics, and alerts are available and how to use them.
Use the following table to understand what information you can learn about your Azure resources.
:::row::: :::column::: Type :::column-end::: :::column span="2"::: Example questions :::column-end::: :::row-end::: :::row::: :::column::: Azure hosting :::column-end::: :::column span="2"::: Hosting services such as Azure App Service and Azure Function provide several forms of feedback to answer questions such as:
* Did my application (or container) deploy successfully?
* Did my application (or container) start successfully?
* Is my application (or container) running successfully?
:::column-end:::
:::row-end::: :::row::: :::column::: Azure service :::column-end::: :::column span="2"::: Azure offers metrics for services, which allows you to get answers such as:
* How busy is the service?
* What errors is the service is producing?
* Is my service so busy it isn't able to keep up with demand?
* Have I reached my pricing tier transaction quota?
:::column-end:::
:::row-end::: :::row::: :::column::: Azure SDK :::column-end::: :::column span="2"::: The Azure SDK provides access to Azure from many programming languages. The SDKs provides logging to allow you to ask questions such as: * What is the SDK itself doing? * What is my code doing? :::column-end::: :::row-end::: :::row::: :::column::: Your code or container :::column-end::: :::column span="2"::: To understand how your own code or container is working, integrate Application Insights from Azure Monitor. Application Insights allows you to capture logs across services for a single application to ask questions such as:
* What exceptions your code throws?
* What events is your code triggering?
* How is your code interacting with dependencies?
:::column-end:::
:::row-end:::
Type | Availability | Description |
---|---|---|
Metrics | Provided without configuration | Start with metrics because every Azure service will have some metrics to allow you to see how it is performing. |
Alerts | Configurable | Configure alerts to be notified when negative or quota-expiring behaviors happen. |
Logging | Configurable | Some services, such as hosting services, have logging to help you understand how your code or container is behaving. You may need to configure logging before you can see log files. |
Custom logging | Configurable via code | From your own code, you can log to Azure Monitor, using Application Insights SDK for server and client application. The code doesn't have to be hosted on Azure to log to Azure Monitor. |
View metrics for your resource on a scheduled basis in the Azure portal in the Monitoring -> Metrics section.
:::image type="content" source="../media/logging-metrics/view-resource-metrics-in-azure-portal.png" alt-text="Screenshot to view metrics for Cognitive Services in Azure portal. ":::
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)
:::image type="content" source="../media/logging-metrics/create-alert-for-http-server-errors-in-app-service.png" alt-text="Set common alerts for your resource such as requests, response time and http server errors (for your hosting environment resources).":::
Hosted applications and containers can be configured to log information about:
- deployment
- start-up
- run-time
Turn on these logs to understand how your hosted application behaves. These logs are probably the first place you'll learn your deployment failed or your start-up configuration is incorrectly configured or missing dependencies.
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 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
Your best and most configurable logging choice is Application Insights, which can provide:
- Standard logging
- in the Azure Service
- in your source code depending on the initialization
- Custom logging
- from your deployment pipeline
- in your source code
To enable application host logging in the Azure portal:
- Go to Monitoring -> App Service logs, enable Application logging for the file system.
- Configure the Quota (MB). The default value is 35.
- Set the Retention Period (Days) value to a default such as 3 or 7 days.
- Select Save to begin capturing host logs.
When you turn on application logs, the logs are stored in the Logs folder of your web app host. View the logs either from the Azure portal or the Visual Studio Code extension for App Service.
Log file name formats include:
- Deployment:
{DATE-TIME}_{RANDOM-CHARS}_{docker}.log
- Start up and runtime:
{DATE-TIME}_{RANDOM-CHARS}_default_docker.log
For App Service, 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 |
See the steps to enable host logging for App Service
With a consumption-based Functions app, if you use logging provided by the context object in your source code, those logs are provided under each function's Monitor section. The Azure portal also allows you to stream the logs as requests come into the function.
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"
:::image type="content" source="../media/logging-metrics/azure-function-app-application-insights-custom-log-kusto-query.png" alt-text="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.":::
If you're 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.
View failures for Application Insights monitored resources.
:::image type="content" source="../media/logging-metrics/view-resource-failure-in-application-insights.png" alt-text="View failures for Application Insights monitored resources.":::