Set up ASP.NET Core instrumentation
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes. The Elastic APM .NET agent enables you to trace the execution of operations in your .NET applications, sending performance metrics and errors to the Elastic APM server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless Observability
Generally available
Stack
Generally available
APM Agent .NET
Generally available
| Framework | Supported versions |
|---|---|
| ASP.NET Core | ≥8.0.0 ≤10.0.x |
For the full compatibility matrix including supported installation methods, refer to Web frameworks.
For ASP.NET Core, once you reference the Elastic.Apm.NetCoreAll package, you can enable every agent component by calling the AddAllElasticApm() extension method on the IServiceCollection in the Program.cs file.
The following code sample assumes the instrumentation of an ASP.NET Core 8 application, using top-level statements.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAllElasticApm();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.Run();
With this you enable every agent component including ASP.NET Core tracing, monitoring of outgoing HTTP request, Entity Framework Core database tracing, and so on.
In case you only reference the Elastic.Apm.AspNetCore package, you won’t find the AddAllElasticApm. Instead you need to use the AddElasticApmForAspNetCore() method. 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 AddElasticApm method.
For example:
using Elastic.Apm.DiagnosticSource;
using Elastic.Apm.EntityFrameworkCore;
builder.Services.AddElasticApm(
new HttpDiagnosticsSubscriber(), /* Enable tracing of outgoing HTTP requests */
new EfCoreDiagnosticsSubscriber()); /* Enable tracing of database calls through EF Core*/
- for HttpDiagnosticsSubscriber
- for EfCoreDiagnosticsSubscriber
In case you only want to use the Public API, you don’t need to do any initialization, you can start using the API and the agent will send the data to the APM Server.
After adding the agent, configure it to connect to your APM Server. The fastest way is through environment variables or appsettings.json. See Minimum configuration for the three settings every deployment needs.
To set configuration values programmatically, for example to derive the APM service name or environment from other application configuration, see Overriding configuration values programmatically.