Azure Web App Logging Tutorial and Troubleshooting Guide

Author

Reads 1.1K

Burning Sticks Beside Rocks
Credit: pexels.com, Burning Sticks Beside Rocks

Logging is an essential part of any web application, and Azure Web Apps provide robust logging capabilities.

Azure Web Apps offer three types of logs: Application Logs, File Logs, and Diagnostic Logs. These logs can be accessed through the Azure portal.

To enable logging in Azure Web Apps, you need to configure the logging settings in the Azure portal. This can be done by going to the Application Settings of your web app and enabling the logging option.

Application Logs contain information about HTTP requests, exceptions, and other important events in your web app.

How to Enable

To enable diagnostic logs for your Azure Web App, you can use the Azure Portal or Azure CLI. The Azure Portal is a user-friendly interface that allows you to easily enable diagnostic logs with just a few clicks.

You can also use the Azure CLI to enable diagnostic logs, but you'll need to use the Set-AzWebApp cmdlet with the correct syntax. Unfortunately, there isn't a cmdlet in the new Az module to enable application diagnostic logs, so you'll need to use the Azure Portal or Azure CLI for now.

If you're using the Azure CLI, you'll need to use the Set-AzWebApp cmdlet with the following syntax to enable diagnostic logs.

Log Configuration and Filtering

Credit: youtube.com, Logging in Azure App Service(Web App)

Log configuration is a crucial aspect of Azure Web App logging. You can record application diagnostic logs by specifying the log level: error, warning, information, and verbose, and storage location: file system or Azure Storage blobs.

To enable application logging, you need to specify the log level and storage location. You can do this by using the Azure Portal or through your application code.

You can also apply filters to reduce the number of logs sent to Dynatrace. There are two options: setting the FILTER_CONFIG environment variable during deployment or adding it after deployment. This will allow you to filter out unwanted logs based on log levels or patterns.

Here are the filter options:

You can set filters for three filter groups: GLOBAL, RESOURCE_TYPE, and RESOURCE_ID. For example, you can set the FILTER.GLOBAL.MIN_LOG_LEVEL to Warning to skip Informational logs.

Configuration

To configure log forwarding, you'll need to set environment variables. This can be done during or after deployment. Set the following variables, replacing the placeholders with your own values: DEPLOYMENT_NAME, TARGET_URL, TARGET_API_TOKEN, RESOURCE_GROUP, EVENT_HUB_CONNECTION_STRING, USE_EXISTING_ACTIVE_GATE, and REQUIRE_VALID_CERTIFICATE.

Credit: youtube.com, Log Filtering and Truncation - How to Configure

You can enable self-monitoring and log filtering during or after deployment. To do this, you'll need to set the DEPLOYMENT_NAME environment variable. This variable holds the name of your deployment.

To deploy the infrastructure, download the azure-log-forwarder-function script and run it. Be sure to check if you want to set other optional parameters as well.

Here are the required environment variables in a list format:

  • DEPLOYMENT_NAME
  • TARGET_URL
  • TARGET_API_TOKEN
  • RESOURCE_GROUP
  • EVENT_HUB_CONNECTION_STRING
  • USE_EXISTING_ACTIVE_GATE
  • REQUIRE_VALID_CERTIFICATE

The azure-log-forwarder-function script is available for download from GitHub. After downloading the script, run it with the required environment variables set.

Custom Metrics with DogStatsD

Custom metrics with DogStatsD can be sent directly to Datadog from Azure Web Apps and Functions with the extension. This is done by adding the DogStatsD NuGet package to your Visual Studio project.

To submit custom metrics to Datadog from Azure App Service using the extension, you need to initialize DogStatsD and write custom metrics in your application. Deploy your code to Azure App Service, and if you haven't already, install the Datadog App Service extension.

Credit: youtube.com, Sending Custom Metrics, tags to DataDog using Python

Note that unlike the standard DogStatsD config process, there is no need to set ports or a server name when initializing the DogStatsD configuration. Ambient environment variables in Azure App Service determine how the metrics are sent (requires v6.0.0+ of the DogStatsD client).

To send metrics, use this code:

To send only custom metrics (while disabling tracing), set the following variables in your application's config:

  • Set DD_TRACE_ENABLED to false.
  • Set DD_AAS_ENABLE_CUSTOM_METRICS to true.

This allows you to customize your metrics without enabling tracing.

Verification

To verify if your Azure web app logging is set up correctly, you need to check for the presence of log lines in Dynatrace. In around 10 minutes, further logs should start coming in.

First, go to Logs or Logs & Events in Dynatrace and confirm that the following log line is present. This indicates that the logging is working as expected.

You can also read Azure Function logs in which the Azure-log-forwarder is running. To do this, enable streaming execution logs in Azure Functions. This will give you a detailed view of what's happening with your logs.

Credit: youtube.com, Using Azure app service logs

Make sure that the Event Hubs instances and the resource group in which the deployment will run are in the same region. This is crucial for the logging to work properly.

Here are some additional things to check if no logs are coming in:

  • The Event Hubs instances and the resource group in which the deployment will run are in the same region.
  • You carefully followed the steps to Configure diagnostic settings.

If you're experiencing SNAT port exhaustion, you'll need to scale your Azure Functions according to the guide below. This is because Azure Functions have a limited number of ports that can be opened at a time (128).

Troubleshooting and Limitations

Troubleshooting is key when it comes to getting the most out of your Azure web app logging. Proper configuration of the Azure integration is crucial to monitor your application, and without it, you're missing critical context for your traces.

To fix this, start by going to the Azure integration tile and ensuring the Azure integration is installed for the subscription where your application is running. Also, make sure any App Service plan filtering rules you have applied include the App Service plan where the app is running.

Credit: youtube.com, Troubleshooting and Viewing Logs of Azure WebApp | Shantanu Das

If you're experiencing issues, here are some key things to check:

As for limitations, it's essential to be aware of the maximum throughput and retention time for Azure log forwarder. Logs older than 24 hours are rejected, and the Azure log forwarder supports a maximum 70 MB per minute (~4 GB per hour) in the default configuration.

CLI Workaround

If you're having trouble accessing your Azure logs, you can try using Azure Cloud Shell as a workaround.

Azure Cloud Shell is a browser-based shell built into Azure Portal. You can use Azure CLI and Azure PowerShell directly from a browser.

To get started, you'll need to store the name of your web app in a variable. This means typing a command like `appname=$(az webapp show --name webappname --resource-group resourcegroupname --query name --output tsv)`.

Once you've set up your variables, you can enable Application Service logs using the command `az webapp log config --resource-group resourcegroupname --name webappname --enabled true`.

Troubleshooting

Credit: youtube.com, Troubleshooting Basics

Troubleshooting can be a real challenge, especially when it comes to Azure integration. You might be missing critical context for your traces if you haven't configured Azure integration properly.

First, make sure you've installed the Azure integration for the Azure subscription where your application is running. This is a crucial step, as it allows you to correlate metrics, traces, and logs in the Datadog platform.

Ensure that any App Service plan filtering rules you have applied include the App Service plan where the app is running. If an App Service plan is not included, all apps and functions hosted on it are also not included.

Here's a quick checklist to help you troubleshoot Azure integration issues:

  1. Go to the Azure integration tile.
  2. Ensure you have installed the Azure integration for the Azure subscription where your application is running.
  3. Ensure that any App Service plan filtering rules you have applied include the App Service plan where the app is running.

By following these steps, you should be able to resolve any Azure integration issues and get the most out of the Datadog platform.

Limitations

Logs older than 24 hours are rejected by the Dynatrace log ingest endpoint, so it's recommended to set a retention time of less than 24 hours for Azure Event Hubs.

The Azure log forwarder has a default configuration that supports a maximum of 70 MB per minute, which is equivalent to about 4 GB per hour. This throughput is measured by the Event Hubs metric Outgoing Bytes of the Event Hubs instance attached to the function.

Helpful AI assistant

Credit: youtube.com, Configure Azure Web App Logging With .NET 5

Having a helpful AI assistant like myself can make a huge difference in your Azure Web App logging experience. I can provide you with personalized guidance and recommendations to help you get the most out of your logging setup.

Azure Web App logging is a complex process, but with the right tools and knowledge, it can be simplified. I can help you navigate the various logging options available in Azure, including Application Insights, which provides rich analytics and monitoring capabilities.

Application Insights is a powerful tool that can help you understand how your web app is performing. It can track user behavior, diagnose issues, and even predict future problems.

If you're not using Application Insights, you're missing out on a lot of valuable insights into your web app's performance. It's a game-changer for any web developer or administrator.

To get started with Application Insights, you'll need to create a resource in Azure and configure it to work with your web app. This involves setting up a few key settings, such as the instrumentation key and the telemetry configuration.

Credit: youtube.com, Azure AI Services Diagnostic Logging Follow Along - Azure AI Engineer Associate (AI-102)

Once you've set up Application Insights, you can start collecting data on your web app's performance. This data can be used to identify trends, diagnose issues, and even optimize your web app for better performance.

I've seen firsthand how Application Insights can transform the way you approach web app development and maintenance. It's a powerful tool that can help you build better, faster, and more reliable web apps.

Calvin Connelly

Senior Writer

Calvin Connelly is a seasoned writer with a passion for crafting engaging content on a wide range of topics. With a keen eye for detail and a knack for storytelling, Calvin has established himself as a versatile and reliable voice in the world of writing. In addition to his general writing expertise, Calvin has developed a particular interest in covering important and timely subjects that impact society.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.