microsoft / ApplicationInsights-Profiler-AspNetCore

Application Insights Profiler sample and documentation
MIT License
66 stars 22 forks source link

Application Insights Profiler for ASP.NET Core

Nuget NuGet-preview

Announcement

Previous announcements

Description

This is the project home page for Microsoft Application Insights Profiler for ASP.NET Core. The NuGet packages can be found here.

Profiler Traces

Get Started

⚠️ These are steps for the ASP.NET Core applications. For the Worker Service, refer to this example.

dotnet new webapi
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.

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();
}

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
    ...
}

In appsettings.Development.json, add the following configuration:

{
    ...
    "ApplicationInsights": {
        "ConnectionString": "replace-with-your-connection-string"
    }
    ...
}

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

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:

Profiler Trace Sessions

Tip: Click on the trace to open the trace analyzer.

Next

Supported Versions

To find out the proper version of the Profiler to use, please refer to Support Matrix.

Examples

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.