Skip to content

microsoft/ApplicationInsights-Profiler-AspNetCore

Repository files navigation

Application Insights Profiler for ASP.NET Core

Nuget NuGet-preview

Announcement

Previous announcements

  • Worker service support is in beta testing now.
    • Check out the example for package info and the instructions.

Description

Welcome to the home page of Microsoft Application Insights Profiler for ASP.NET Core. You can find the NuGet packages here.

Profiler Traces

Getting Started

⚠️ These instructions are for ASP.NET Core applications. For Worker Services, refer to this example.

  1. Create a New Application

    For example:

    dotnet new webapi
  2. Add NuGet Packages

    dotnet add package Microsoft.ApplicationInsights.AspNetCore
    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
  3. Enable Application Insights Profiler

    Modify Startup.cs or Program.cs to include:

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        // Add Application Insights and Profiler
        services.AddApplicationInsightsTelemetry();
        services.AddServiceProfiler();
    }
  4. Optionally, Add a Bottleneck

    • Add a reference to HeavyArrayForcast, for example, in QuickStart3_1.csproj:

      <ItemGroup>
          <ProjectReference Include="..\PerfIssueLib\HeavyArrayForecast\HeavyArrayForecast.csproj" />
      </ItemGroup>
    • Use it in WeatherForecastController.cs:

      using HeavyArrayForecast;
      ...
      
      [HttpGet]
      public IEnumerable<WeatherForecast> Get()
      {
          return WeatherForecastHelper.GetForecasts();
      }

    Note: Check WeatherForecastHelper.cs for details on the issue. If you prefer not to use project-to-project references, you can copy the code from this file directly into your project.

  5. Set Up Connection String:

    In appsettings.Development.json, add:

    {
        ...
        "ApplicationInsights": {
            "ConnectionString": "replace-with-your-connection-string"
        }
        ...
    }
  6. Run and Profile the Application

    Start your application:

    dotnet run

    The profiler will automatically start for 2 minutes. Access the endpoint during this period:

    https://localhost:5001/weatherforecast

Trace Analysis

  1. Analyze Traces in Azure Portal

    Wait a few minutes for data ingestion. In the Azure Portal, navigate to your Application Insights resource, go to the Performance blade, and click Profiler to view profiling sessions:

    Profiler Trace Sessions

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

  2. Review Code Optimizations

    Once Profiler traces are available in the Azure portal for your application, they are automatically analyzed by an AI-based service known as Code Optimizations. This service helps detect and remediate performance bottlenecks at the code level for your running .NET applications. This analysis does not incur any additional overhead on your server or costs to your bill.

    To review the available Code Optimizations:

    1. Navigate to the Performance blade.
    2. You might see the top 3 recommendations in the Insights card.
    3. Alternatively, click on the Code Optimizations button.

    Note that it may take a few hours for Code Optimizations to appear.

    Code Optiizations

    For more information, refer to the Code Optimizations Documentation.

Next Steps

Supported Versions

Refer to the Support Matrix to determine the appropriate profiler version.

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 [email protected] with any additional questions or comments.