This is a cache of https://www.elastic.co/guide/en/apm/agent/dotnet/current/setup-asp-net-core.html. It is a snapshot of the page at 2024-11-06T01:03:21.844+0000.
ASP.NET Core | APM .NET Agent Reference [1.x] | Elastic

ASP.NET Core

edit

Quick start

edit

We strongly suggest using the approach described in the .NET applications using Microsoft.Extensions.Hosting instructions, to register the agent on the IServiceCollection, as opposed to using IApplicationBuilder as described below.

We keep the IApplicationBuilder introduced here only for backwards compatibility.

For ASP.NET Core, once you reference the Elastic.Apm.NetCoreAll package, you can enable auto instrumentation by calling the UseAllElasticApm() extension method:

using Elastic.Apm.NetCoreAll;

public class Startup
{
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
    app.UseAllElasticApm(Configuration);
    //…rest of the method
  }
  //…rest of the class
}

The app.UseAllElasticApm(...) line must be the first line in the Configure method, otherwise the agent won’t be able to properly measure the timing of your requests, and complete requests may potentially be missed by the agent.

With this you enable every agent component including ASP.NET Core tracing, monitoring of outgoing HTTP request, Entity Framework Core database tracing, etc.

In case you only reference the Elastic.Apm.AspNetCore package, you won’t find the UseAllElasticApm. Instead you need to use the UseElasticApm() method from the Elastic.Apm.AspNetCore namespace. This method turns on ASP.NET Core tracing, and gives you the opportunity to manually turn on other components. By default it will only trace ASP.NET Core requests - No HTTP request tracing, database call tracing or any other tracing component will be turned on.

In case you would like to turn on specific tracing components you can pass those to the UseElasticApm method.

For example:

app.UseElasticApm(Configuration,
	new HttpDiagnosticsSubscriber(),  /* Enable tracing of outgoing HTTP requests */
	new EfCoreDiagnosticsSubscriber()); /* Enable tracing of database calls through EF Core*/

In case you only want to use the Public API, you don’t need to do any initialization, you can simply start using the API and the agent will send the data to the APM Server.