Enable Profiler for ASP.NET Core web applications hosted in Linux on App Services

Using Profiler, 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. While this guide focuses on web apps hosted in Linux, you can experiment using Linux, Windows, and Mac development environments.

In this guide, you'll:

  • Set up and deploy an ASP.NET Core web application hosted on Linux.
  • Add Application Insights Profiler to the ASP.NET Core web application.

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 package to collect the Profiler traces:

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
    
  5. In your preferred code editor, enable Application Insights and Profiler in Program.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry(); // Add this line of code to enable Application Insights.
        services.AddServiceProfiler(); // Add this line of code to Enable Profiler
        services.AddControllersWithViews();
    }
    
  6. Add a line of code in the HomeController.cs section to randomly delay a few seconds:

    using System.Threading;
    ...
    
    public IActionResult About()
        {
            Random r = new Random();
            int delay = r.Next(5000, 10000);
            Thread.Sleep(delay);
            return View();
        }
    
  7. 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 of 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 note of your credentials to use later.

    Screenshot of creating the deployment credentials.

  3. Click Save.

  4. Select the Settings tab.

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

    Screenshot of view deployment options in a drop-down.

  6. Click Save to create a Git repository with a Git Clone Uri.

    Screenshot of setting up the local Git repository.

    For more deployment options, see App Service documentation.

Deploy your project

  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.azurewebsites.net: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
    

    You should see output similar to the following example:

    Counting objects: 9, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (8/8), done.
    Writing objects: 100% (9/9), 1.78 KiB | 911.00 KiB/s, done.
    Total 9 (delta 3), reused 0 (delta 0)
    remote: Updating branch 'main'.
    remote: Updating submodules.
    remote: Preparing deployment for commit id 'd7369a99d7'.
    remote: Generating deployment script.
    remote: Running deployment command...
    remote: Handling ASP.NET Core Web Application deployment.
    remote: ......
    remote:   Restoring packages for /home/site/repository/EventPipeExampleLinux.csproj...
    remote: .
    remote:   Installing Newtonsoft.Json 10.0.3.
    remote:   Installing Microsoft.ApplicationInsights.Profiler.Core 1.1.0-LKG
    ...
    

Add Application Insights to monitor your web app

You can add Application Insights to your web app either via:

  • The Application Insights pane in the Azure portal,
  • The Configuration pane in the Azure portal, or
  • Manually adding to your web app settings.
  1. In your web app on the Azure portal, select Application Insights in the left side menu.

  2. Click Turn on Application Insights.

    Screenshot of turning on Application Insights.

  3. Under Application Insights, select Enable.

    Screenshot of enabling Application Insights.

  4. Under Link to an Application Insights resource, either create a new resource or select an existing resource. For this example, we'll create a new resource.

    Screenshot of linking your Application Insights to a new or existing resource.

  5. Click Apply > Yes to apply and confirm.

Next steps

Learn how to...