Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
You can enable the Application Insights Profiler for .NET on applications running in your container almost without code. To enable the .NET Profiler on your container instance, you need to:
- Add the reference to the
Microsoft.ApplicationInsights.Profiler.AspNetCore
NuGet package. - Update the code to enable the Profiler for .NET.
- Set up the Application Insights connection string.
In this article, you learn about the various ways that you can:
- Install the NuGet package in the project.
- Set the environment variable via the orchestrator (like Kubernetes).
- Learn security considerations around production deployment, like protecting your Application Insights connection string.
Prerequisites
- An Application Insights resource. Make note of the connection string.
- Docker Desktop to build Docker images.
- .NET 6 SDK installed.
Set up the environment
Clone and use the following sample project:
git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
Go to the Container App example:
cd examples/EnableServiceProfilerForContainerAppNet6
This example is a barebones project created by calling the following CLI command:
dotnet new mvc -n EnableServiceProfilerForContainerApp
A delay is in the
Controllers/WeatherForecastController.cs
project 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)); }
Add the NuGet package to collect the .NET Profiler traces:
dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
Enable Application Insights and the .NET Profiler.
Add
builder.Services.AddApplicationInsightsTelemetry()
andbuilder.Services.AddServiceProfiler()
after theWebApplication.CreateBuilder()
method inProgram.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();
You can also add custom Profiler settings, if applicable.
Pull the latest ASP.NET Core build/runtime images
Go to the .NET Core 6.0 example directory:
cd examples/EnableServiceProfilerForContainerAppNet6
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
Add your Application Insights key
Via your Application Insights resource in the Azure portal, take note of your Application Insights connection string.
Open
appsettings.json
and add your Application Insights connection string to this code section:{ "ApplicationInsights": { "InstrumentationKey": "Your connection string" } }
Build and run the Docker image
Review the Docker file.
Build the example image:
docker build -t profilerapp .
Run the container:
docker run -d -p 8080:80 --name testapp profilerapp
View the container via your browser
To hit the endpoint, you have two options:
Visit
http://localhost:8080/weatherforecast
in your browser.Use curl:
curl http://localhost:8080/weatherforecast
Inspect the logs
Optionally, inspect the local log to see if a session of profiling 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.
Troubleshooting
If you are unable to find traces from your app, consider following the steps in this troubleshooting guide.
View the .NET Profiler traces
Wait for 2 to 5 minutes so that the events can be aggregated to Application Insights.
Open the Performance pane in your Application Insights resource.
After the trace process is finished, the Profiler Traces button appears.
Clean up resources
Run the following command to stop the example project:
docker rm -f testapp