Enable the .NET Profiler for Azure App Service apps in Linux

Using Application Insights Profiler for .NET, you can track how much time is spent in each method of your live ASP.NET Core web apps that are hosted in Linux on Azure App Service. This article focuses on web apps hosted in Linux. You can also experiment by using Windows and Mac development environments.

In this article, you:

  • Set up an ASP.NET Core web application hosted on Linux on your local computer.
  • Create an App Service using the Azure portal.
  • Deploy your local ASP.NET Core project to Azure using local Git.
  • Add the Profiler to the ASP.NET Core web application.

Note

Review the Application Insights SDK support guidance for our Classic API SDK support policy.

Caution

We recommend the Azure Monitor OpenTelemetry Distro for new applications or customers to power Azure Monitor Application Insights. The Azure Monitor OpenTelemetry Distro delivers a similar functionality and experience as the Application Insights SDK. It's possible to migrate from the Application Insights SDK using the migration guides for .NET, Node.js, and Python, but we are still working to add a few more features for backwards compatibility.

Prerequisites

Set up the project locally

  1. Open a command prompt window on your machine.

  2. Create an ASP.NET Core MVC web application:

    dotnet new mvc -n LinuxProfilerTest
    
  3. Change the working directory to the root folder for the project.

  4. Add the NuGet packages to collect the Profiler traces:

    dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore --prerelease
    dotnet add package Azure.Monitor.OpenTelemetry.Profiler --prerelease
    

Enable the .NET Profiler

  1. In your preferred code editor, verify the two packages for the Azure Monitor OpenTelemetry Profiler for .NET were added to Program.cs. Add custom Profiler settings, if applicable.

    In your project's .csproj file, verify the following lines have been added:

    <ItemGroup>
        <PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="[1.*-*, 2.0.0)" />
        <PackageReference Include="Azure.Monitor.OpenTelemetry.Profiler" Version="[1.*-*, 2.0.0)" />
    </ItemGroup>
    

    In your Program.cs file, verify the following lines have been added:

    using Azure.Monitor.OpenTelemetry.AspNetCore;
    using Azure.Monitor.OpenTelemetry.Profiler;
    
    ///
    
    builder.Services.AddOpenTelemetry()
        .UseAzureMonitor()          // Enable Azure Monitor OpenTelemetry distro for ASP.NET Core
        .AddAzureMonitorProfiler(); // Add Azure Monitor Profiler    
    
  2. Save and commit your changes to the local repository:

    git init
    git add .
    git commit -m "first commit"
    

Create the Linux web app to host your project

  1. In the Azure portal, create a web app environment by using App Service on Linux.

    Screenshot that shows creating the Linux web app.

  2. Go to your new web app resource and select Deployment Center > FTPS credentials to create the deployment credentials. Make a note of your credentials to use later.

    Screenshot that shows creating the deployment credentials.

  3. Select Save.

  4. Select the Settings tab.

  5. In the dropdown, select Local Git to set up a local Git repository in the web app.

    Screenshot that shows view deployment options in a dropdown.

  6. Select Save to create a Git repository with a Git clone URI.

    Screenshot that shows setting up the local Git repository.

Deploy your project

While you can deploy code to Azure App Service a variety of ways, the simplest way is deploy via local Git. Learn more about how to deploy from a Git repository to Azure.

  1. In your command prompt window, browse to the root folder for your project. Add a Git remote repository to point to the repository on App Service:

    git remote add azure https://<username>@<app_name>.scm.chinacloudsites.cn:443/<app_name>.git
    
    • Use the username that you used to create the deployment credentials.
    • Use the app name that you used to create the web app by using App Service on Linux.
  2. Deploy the project by pushing the changes to Azure:

    git push azure main
    

Add Application Insights to monitor your web app

You can enable Application Insights while creating an App Service, which sets the connection string automatically.

Copy and paste your connection string from your Application Insights resource to monitor your web app.

  1. Copy the connection string.
  2. Paste the connection string into your environment.

Troubleshooting

If you are unable to find traces from your app, consider following the steps in this troubleshooting guide.

Next steps