Contact Us

Deploy ASP.NET Core Applications to Azure App Service | Datadog

Mobile App | October 14, 2021

The ASP.NET Core framework provides cross-platform support for web development, giving you greater control over how you build and deploy your .NET applications. With the ability to run .NET applications on more platforms, you need to ensure that you have visibility into application performance, regardless of where your applications are hosted.

In previous posts, we looked at instrumenting and monitoring a .NET application deployed via Docker and AWS Fargate. In this post, we’ll show how Datadog can help you get visibility into a .NET Core application on Azure App Service (AAS), a cloud-based platform-as-a-service (PaaS) for deploying web and mobile applications and other resources.

We’ll walk through:

Datadog’s Azure integration enables you to capture metrics and logs from all of your resources on Azure, giving you visibility into the performance of your workloads and their underlying infrastructure. Datadog’s extension for Azure App Service provides out-of-the-box instrumentation for .NET applications on AAS as well as support for custom instrumentation, trace ID injection, and application runtime metrics. This enables you to track requests as they move across service and process boundaries, correlate that data with code-level performance, and quickly identify any bottlenecks.

To get started, make sure that you have at least version 5 of the .NET Core SDK installed, which includes the .NET CLI. The CLI lets you generate the sample ASP.NET Core application we’ll use throughout this guide. You will also need the Azure CLI in order to publish the application to Azure App Service.

Create a new web application project with all of the files needed to run a sample application via the following .NET CLI commands:

 
dotnet new sln -n DatadogAasExample
dotnet new webapp -o DatadogAasExample -n DatadogAasExample
dotnet sln add DatadogAasExample

These commands create a new solution file (i.e.,DatadogAasExample.sln) and add a new Razor Pages web application project and associated DatadogAasExample directory to the file. You can build and run the project locally by running the following command:

 
dotnet run --project ./DatadogAasExample -- --URLS http://localhost:8000

You can then view your application by navigating to http://localhost:8080/.

Next, we’ll show you how to set up Azure App Service to publish your application. Once you’ve done this, you can deploy your application directly to AAS, which will automatically create the runtime environment necessary to run it.

Publishing applications on AAS requires an App Service, which includes a resource group and host plan. The resource group is a collection of all of your application resources, such as databases or APIs. The Azure App Service host plan defines the makeup of your application’s compute resources, such as the number of virtual machine instances that should be created along with their operating system, region, and size.

You can create a new resource group and host plan and publish your application via the Azure CLI. First, log into your Azure account, then run the following CLI command in the DatadogAasExample project folder to create a new App Service, prepare the application, and publish it to AAS:

 
az webapp up --sku F1 -n Datadog-MySampleApp --os-type windows -g Datadog-MySampleApp-RG -p Datadog-MySampleApp-AAS

You can append --dryrun to the command to view more information about what the command will do before it runs, but we’ll look at each parameter in more detail below.

To learn more about the CLI’s commands and optional parameters, check out Azure’s documentation.

When you run the command, Azure App Service automatically detects that you are deploying a .NET Core application and hosts it on a free-tier shared VM instance with the appropriate OS and runtime. The CLI will provide more details about your deployed application in logs similar to the following output:

 
The webapp 'Datadog-MySampleApp' doesn't exist
Creating Resource group 'Datadog-MySampleApp-RG' ...
Resource group creation complete
Creating AppServicePlan 'Datadog-MySampleApp-AAS' ...
Creating webapp 'Datadog-MySampleApp' ...
Configuring default logging for the app, if not already enabled
Creating zip with contents of dir /app/DatadogAasExample ...
Getting scm site credentials for zip deployment
Starting zip deployment. This operation can take a while to complete ...
Deployment endpoint responded with status code 202
You can launch the app at http://datadog-mysampleapp.azurewebsites.net
{
  "URL": "http://datadog-mysampleapp.azurewebsites.net",
  "appserviceplan": "Datadog-MySampleApp-AAS",
  "location": "centralus",
  "name": "Datadog-MySampleApp",
  "os": "Windows",
  "resourcegroup": "Datadog-MySampleApp-RG",
  "runtime_version": "dotnet|5.0",
  "runtime_version_detected": "5.0",
  "sku": "FREE",
  "src_path": "//app//DatadogAasExample"
}

To view your application on AAS, navigate to the URL in the log output (i.e., http://datadog-mysampleapp.azurewebsites.net).

Next, we’ll look at how you can get visibility into your Azure environment and .NET application with Datadog’s Azure integration and extension for Azure App Service.

Datadog’s Azure integration collects metrics and logs from your Azure services, including Azure App Service, and is required for the extension. You can enable the integration via the Azure portal or CLI, but we’ll use the CLI in this guide.

The Azure integration requires creating an app registration, which uses a service principal to generate a set of credentials that grant Datadog monitoring access to your application’s subscription.

To create a new service principal, run the following command and replace {subscription_id} with your application’s subscription ID, which you can find via the az account show CLI command:

az ad sp create-for-rbac --role "Monitoring Reader" --scopes /subscriptions/{subscription_id} --name datadog-monitor-sp

The service principal is configured with the “Monitoring Reader” role and scoped to your application’s subscription. Once the CLI creates the new service principal, it will display credentials similar to the following output:

 
{
  "appId": "12c406f7-e9aa-4b43-1234-df0aafd7cf9c",
  "displayName": "datadog-monitor-sp",
  "name": "f12345a1-7e8d-4dcc-8e3e-8f1e12e3db45",
  "password": "XYabcdEFGd3JId.FDSg1234.GRP",
  "tenant": "e12ef3b4-5e67-8b90-b1a0-1234abcd9c787"
}

Enter the appId, tenant, and password values into the Client ID, Tenant ID, and Client Secret fields respectively in your account’s Azure Integration tile to grant Datadog access to Azure.

Datadog will start collecting standard metrics from Azure Monitor and generate additional metrics and tags from all of your Azure services, such as service-tier tags (e.g., basic, standard, premium) and metrics that track the number of hosts on your Azure App Service host plan.

The Azure integration also enables you to install the extension for Azure App Service, which we’ll look at next.

Azure App Service allows you to add third-party services to your application’s environment via extensions, so you can leverage features such as automatic instrumentation and trace collection for monitoring application performance. The Datadog extension for Azure App Service supports several .NET runtimes running on Windows instances, including the .NET Core framework. Note that AAS does not currently support extensions for Linux-based applications.

Before installing the extension, you will need to configure your application with the following environment variables:

You can add these environment variables to your application via the Azure portal or by running the following Azure CLI command:

az webapp config appsettings set -g Datadog-MySampleApp-RG -n Datadog-MySampleApp --settings DD_API_KEY={your_API_key} DD_ENV=aas_test DD_SERVICE=MySampleApp DD_VERSION=1.0.0 DD_SITE=datadoghq.com DD_TRACE_ROUTE_TEMPLATE_RESOURCE_NAMES_ENABLED=true

Check out our documentation to learn more about the available options for customizing the extension’s tracing configuration.

Now that your application is configured with the appropriate Datadog environment variables, you can install the extension via deployment scripts or one of Azure’s UI interfaces.

Datadog provides Powershell scripts that enable you to easily install and keep extensions up to date from the command line. This streamlines the setup process and makes it easier to manage installed extensions for each of your Azure App Services and resource groups.

To install the extension via Powershell, download the script to the DatadogAasExample project folder and execute it with the following command:

.install-latest-extension.ps1

 
.install-latest-extension.ps1 -Username $username -Password $password -SubscriptionId $subscriptionId -ResourceGroup $resourceGroupName -SiteName $webAppName -DDApiKey $ddApiKey -DDSite $ddSite -DDEnv $ddEnv -DDService $ddService -DDVersion $ddVersion

The script uses your user-scope credentials and configuration data for your application to install and configure Datadog’s out-of-the-box instrumentation for each of your App Service applications, so you can view traces in Datadog APM.

Datadog’s script can be used to update all existing extensions in a resource group to the latest available version. You can run the following command to execute it:

.update-all-site-extensions.ps1

 
.update-all-site-extensions.ps1 -SubscriptionId $subscriptionId -ResourceGroup $resourceGroupName -Username $username -Password $password

You can also install the extension through one of Azure’s UI interfaces, which provide more visibility into your application’s configuration settings and performance.

The Azure portal allows you to view and manage all of your applications, extensions, and application resources in one place. To get started, run the following command to fully stop your application, which is required before installing any new extensions for Azure App Service via the UI:

 
az webapp stop -n Datadog-MySampleApp -g Datadog-MySampleApp-RG 

Next, log into the portal and navigate to the “Azure App Services” blade to find and select your application. Click “Extension” under “Development Tools” and click “Add” to see a list of available extensions. Select the .NET Datadog APM extension to add it to your application.

You can restart your application with the following command to complete the installation process:

 
az webapp start -n Datadog-MySampleApp -g Datadog-MySampleApp-RG

You can also view and install extensions via Kudu, a source code management (SCM) tool for Azure App Service. Every application on AAS includes a Kudu SCM portal, which provides useful information about your application (e.g., environment variables, application settings, server configurations). You can add the Datadog extension by searching for it in the “Gallery” tab on the “Site extensions” page.

Once the extension is installed for your application, Datadog will automatically collect application traces and forward them to Datadog, where you can explore them alongside other performance data.

Datadog’s Azure integration provides a built-in dashboard for Azure App Service, where you can get a high-level overview of all of your applications and service plans.

You can also collect additional data from your application, such as Azure Platform logs, and connect trace IDs to logs, so you can easily correlate traces with log data. This enables you to view application traces and associated logs in Datadog APM and drill down to specific traces to determine if your application is handling requests as expected.

Your trace and log data provide you with more context for troubleshooting a performance issue with your application and pinpointing its root cause, be it an application bug or a problem with an application resource, such as an overloaded server or a slow database query.

Datadog’s extension also includes support for DogStatsD, so you can collect custom metrics that help you measure key performance indicators specific to your application. This data can include anything critical to your business, from the number of completed transactions to the performance of a custom application service.

This content was originally published here.