Uredi

Enable the .NET Profiler on Azure containers

Application Insights Profiler for .NET captures performance traces from your running ASP.NET Core application to show which code paths slow it down under load. When your app runs in a container, you can enable the Profiler with minimal code changes to find and fix these performance bottlenecks in production.

This article shows you how to add the Profiler to a containerized ASP.NET Core app, connect it to your Application Insights resource, and view the traces the Profiler collects.

In this article, you:

  • Install the NuGet package and enable the Profiler in your project.
  • Set the Application Insights connection string in your app settings.
  • Build and run the container, then view the .NET Profiler traces.

Prerequisites

Set up the sample project and enable the .NET Profiler

  1. Clone and use the following sample project:

    git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
    
  2. Go to the Container App example:

    cd ApplicationInsights-Profiler-AspNetCore
    
    cd examples/EnableServiceProfilerForContainerAppNet6
    
  3. Run the following CLI command to create this barebones example project:

    dotnet new mvc -n EnableServiceProfilerForContainerApp
    

    The Controllers/WeatherForecastController.cs file includes a delay to simulate the bottleneck.

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        SimulateDelay();
        ...
        // Other existing code.
    }
    private void SimulateDelay()
    {
        // Delay for 500ms to 2s to simulate a bottleneck.
        Thread.Sleep((new Random()).Next(500, 2000));
    }
    
  4. Add the NuGet package to collect the .NET Profiler traces:

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
    
  5. Enable Application Insights and the .NET Profiler.

    Add builder.Services.AddApplicationInsightsTelemetry() and builder.Services.AddServiceProfiler() after the WebApplication.CreateBuilder() method in Program.cs:

    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddApplicationInsightsTelemetry(); // Add this line of code to enable Application Insights.
    builder.Services.AddServiceProfiler(); // Add this line of code to enable Profiler
    builder.Services.AddControllersWithViews();
    
    var app = builder.Build();
    

    For custom settings, see Customize Application Insights Profiler.


Pull the latest ASP.NET Core 6.0 build and runtime images

  1. Go to the .NET Core 6.0 example directory:

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  2. Pull the latest ASP.NET Core images:

    docker pull mcr.microsoft.com/dotnet/sdk:6.0
    docker pull mcr.microsoft.com/dotnet/aspnet:6.0
    

Tip

Find the official Docker images for the .NET SDK and ASP.NET Core runtime.

Add your Application Insights connection string

  1. In the Azure portal, open your Application Insights resource. In the Overview page, note your Application Insights connection string.

    Screenshot that shows finding the connection string in the Azure portal.

  2. Open appsettings.json and add your Application Insights connection string to this code section:

    {
        "ApplicationInsights":
        {
            "ConnectionString": "Your connection string"
        }
    }
    

Build and run the Docker image

  1. Review the Docker file.

  2. Build the example image:

    docker build -t profilerapp .
    
  3. Run the container:

    docker run -d -p 8080:80 --name testapp profilerapp
    

View the container in your browser

To reach the sample app's weatherforecast endpoint, you have two options:

  • Visit http://localhost:8080/weatherforecast in your browser.

  • Use curl:

    curl http://localhost:8080/weatherforecast
    

Inspect the container logs for a profiling session

Optionally, inspect the local log to see if a .NET Profiler session finished:

docker logs testapp

In the local logs, note the following events:

Starting application insights profiler with connection string: your-connection string # Double check the connection string
Service Profiler session started.               # Profiler started.
Finished calling trace uploader. Exit code: 0   # Uploader is called with exit code 0.
Service Profiler session finished.              # A profiling session is completed.

Troubleshoot missing .NET Profiler traces

If you can't find traces from your app, consider following the steps in this troubleshooting guide.

View the .NET Profiler traces

  1. Wait for two to five minutes so that Application Insights can aggregate the events.

  2. In the Azure portal, open your Application Insights resource. From the left menu, select Investigate > Performance.

  3. After the trace process finishes, the Profiler Traces button appears.

    Screenshot that shows the .NET Profiler traces button in the Performance pane.

Clean up resources

Run the following command to stop the example project:

docker rm -f testapp

Next step