Monitoring application performance is essential for catching database bottlenecks before they impact users. Entity Framework Core (EF Core) exposes performance data natively through the standard .NET System.Diagnostics.Metrics API under the meter name Microsoft.EntityFrameworkCore.
By integrating this meter with OpenTelemetry, you can automatically track active database connections, query volumes, and compiled query cache performance across your application.
Step-by-Step Implementation
1.Install Required Packages:Terminal command.
Run the following command to add the OpenTelemetry hosting and console exporter packages to your project:
1 2 3 | dotnet add package OpenTelemetry.Extensions.Hosting dotnet add package OpenTelemetry.Exporter.Console |
Verification: Check your .csproj file to confirm that OpenTelemetry.Extensions.Hosting and the exporter packages are successfully listed under <ItemGroup>.
2.Configure OpenTelemetry Metrics in Program.cs:Code setup.
Register OpenTelemetry metrics during application startup and subscribe to the EF Core meter using .AddMeter("Microsoft.EntityFrameworkCore"):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using OpenTelemetry.Metrics; var builder = WebApplication.CreateBuilder(args); // Configure OpenTelemetry Metrics builder.Services.AddOpenTelemetry() .WithMetrics(metrics => metrics .AddAspNetCoreInstrumentation() // Captures HTTP performance metrics .AddRuntimeInstrumentation() // Captures .NET runtime metrics (memory, CPU) .AddMeter("Microsoft.EntityFrameworkCore") // Captures EF Core built-in metrics .AddConsoleExporter()); // Exports metrics to the console for local verification var app = builder.Build(); // ... configure middleware and endpoints app.Run(); |
Verification: Run dotnet build to verify that your project compiles cleanly without missing namespace references.
3.Verify Metric Collection:Runtime check.
Start your application and trigger a few API endpoints that execute database operations.
Verification: Inspect your application console output to confirm that performance counters like query counts and active contexts are actively streaming.
Key Metrics Tracked by EF Core
Once configured, EF Core automatically feeds the following performance measurements into your OpenTelemetry pipeline:
| Metric Name | Instrument Type | Description |
microsoft.entityframeworkcore.active_dbcontexts | ObservableUpDownCounter | Tracks the number of currently active DbContext instances. |
microsoft.entityframeworkcore.queries | ObservableCounter | Cumulative count of total database queries executed. |
microsoft.entityframeworkcore.savechanges | ObservableCounter | Cumulative count of database save operations executed. |
microsoft.entityframeworkcore.compiled_query_cache_hits | ObservableCounter | Tracks cache hits for compiled queries. |
microsoft.entityframeworkcore.compiled_query_cache_misses | ObservableCounter | Tracks cache misses for compiled queries. |
Production Setup: Exporting to Prometheus
In production environments, you typically replace the console exporter with a Prometheus exporter to visualize your metrics in Grafana:
1 2 | dotnet add package OpenTelemetry.Exporter.Prometheus.AspNetCore |
Update your metrics registration in Program.cs:
1 2 3 4 5 6 7 8 | builder.Services.AddOpenTelemetry() .WithMetrics(metrics => metrics .AddMeter("Microsoft.EntityFrameworkCore") .AddPrometheusExporter()); // Exposes a /metrics scraping endpoint // Map the scraping endpoint for Prometheus app.MapPrometheusScrapingEndpoint(); |
Best and Cheap Entity Framework Core 10 Hosting
To help you find the best Windows hosting provider that fully support Entity Framework, we will give you our recommendation. After we had reviewed 50+ providers, we found that ASPHostPortal is one of the best. They have expert ASP.NET support who always ready to help your problem, although with 99.99% uptime we think you won’t get any problem.
ASPHostPortal.com is Microsoft No #1 Recommended Windows Hosting Partner ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for the ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, .NET 4.5.2/ASP.NET 4.5.1, ASP.NET MVC 6.0/5.2, Silverlight 5 and Visual Studio Lightswitch.
