Application Insights Profiler for ASP.NET Core
Announcement
- Worker service support is in beta testing now.
- Check out the example for package info and the instructions.
Previous announcements
- Profiler 2.4.0 is released.
Description
This is the project home page for Microsoft Application Insights Profiler for ASP.NET Core
. The NuGet packages can be found here.
Get Started
⚠️ These are steps for the ASP.NET Core applications. For the Worker Service, refer to this example.
- Create an application
dotnet new webapi
- Add NuGet packages
dotnet add package Microsoft.ApplicationInsights.AspNetCore
dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
Notice: .NET Core 2.2 is out of support, it is recommended to migrate your project to .NET 6.x. Refer to .NET Core page for details. If you have to stay on .NET Core 2.2 for now, please this specific version of Microsoft.ApplicationInsights.AspNetCore v2.14
alone with the profiler package.
Tips: Find official migration documentation here.
- Enable Application Insights Profiler
Open Startup.cs
using System.Diagnostics;
...
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
// Adding the following lines to enable application insights and profiler.
services.AddApplicationInsightsTelemetry();
services.AddServiceProfiler();
}
- Add a bottleneck
To make it real, make use the following code to add some delay in the WeatherForecastController.cs to simulate the bottleneck:
using System.Threading;
...
private static void SimulateDelay()
{
// Delay for 200ms to 5s to simulate a bottleneck.
Thread.Sleep((new Random()).Next(200, 5000));
}
And call it from the controller methods:
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
SimulateDelay();
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
...
}
- Setup the instrumentation key for debugging
In appsettings.Development.json, add the following configuration:
{
...
"ApplicationInsights": {
"InstrumentationKey": "replace-with-your-instrumentation-key"
}
...
}
-
Alternatively, use a connection string like in QuickStart3.1 example.
-
Run the WebAPI, generate traffic for profiling
To run your application locally:
dotnet run
At the beginning of the application, OneTimeSchedulingPolicy will immediately kick in, profiling for 2 minutes. Hit the endpoint in a browser during the profiling session in your browser:
https://localhost:5001/weatherforecast
- Analyze the trace from Azure Portal
Get a coffee and wait for a couple of minutes - the backend needs some time, usually a 2 minutes, to ingest the data.
Then, open the application insights resource in Azure Portal, go to the performance blade, and use the button of Configure Profiler
. There, you are going to see all profiling sessions:
Tip: Click on the trace to open the trace analyzer.
Next
- Configurations for the Profiler - describes how to customize various settings of the profiler.
- Dockerize an application with Profiler
- Profiler Sessions - describes when the profiler starts, stops and what is traced.
- Trace Analysis - introduce the trace analysis, get a basic understanding of
AWAIT TIME
,Blocked time
,Unmanaged Async
, etc. - Diagnosing a WebAPI experiencing intermittent high CPU using the Application Insights Profiler.
- The call tree filter.
Supported Versions
To find out the proper version of the Profiler to use, please refer to Support Matrix.
Examples
-
Enable Service Profiler for containerized ASP.NET Core application (.NET Core 3.x).
-
Enable Service Profiler for ASP.NET Core application in Visual Studio.
References
CAUTION
This is a documentation/sample repository. The LICENSE covers the content in this repository but does NOT cover the use of the product of Microsoft.ApplicationInsights.Profiler.AspNetCore. Please reference EULA-prerelease.md for any prerelease product and EULA-GA.md for any non-prerelease product.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.