AppMetrics Library for Monitoring C# (.NET) Applications
When developing modern .NET applications, it is critically important to track their state, performance, and identify bottlenecks. The AppMetrics library is a powerful and flexible tool for collecting, aggregating, and exporting metrics in real time. It allows developers and administrators to get a complete picture of the service's operation: from request execution time to error frequency.
AppMetrics supports many backends for storing metrics: Prometheus, InfluxDB, Graphite, Elasticsearch, and others. Thanks to built-in Health Checks, you can not only collect statistics but also check the health of dependencies (databases, external APIs).
Key Features
- Counters — number of requests, errors, events.
- Histograms — distribution of operation execution time.
- Timers — measuring code execution duration.
- Meters — rate of events per second.
- Health Checks — checking the availability of external services.
- Metrics Export — to Prometheus (/metrics format), InfluxDB, and others.
Installation and Setup
Install the package via NuGet:
dotnet add package AppMetrics.AspNetCore.Mvcdotnet add package AppMetrics.Reporting.Prometheus.ClientThen add services in Program.cs (or Startup.cs):
using App.Metrics;using App.Metrics.AspNetCore;using App.Metrics.Formatters.Prometheus;
var builder = WebApplication.CreateBuilder(args);
// Metrics configurationvar metrics = AppMetrics.CreateDefaultBuilder() .OutputMetrics.AsPrometheusPlainText() .Build();
builder.Services.AddMetrics(metrics);builder.Services.AddMetricsTrackingMiddleware();builder.Services.AddMetricsEndpoints();
var app = builder.Build();
app.UseMetricsAllMiddleware();app.UseMetricsAllEndpoints();
app.MapGet("/", () => "Hello World!");
app.Run();After starting the application, metrics will be available at /metrics in Prometheus format.
Creating Custom Metrics
Define a class for storing metrics:
public class MyMetrics{ private readonly IMetrics _metrics;
public MyMetrics(IMetrics metrics) { _metrics = metrics; }
// Request counter private readonly CounterOptions _requestCounter = new CounterOptions { Name = "myapp_requests_total", MeasurementUnit = Unit.Requests };
// Timer for measuring execution time private readonly TimerOptions _requestTimer = new TimerOptions { Name = "myapp_request_duration_seconds", MeasurementUnit = Unit.Requests, DurationUnit = TimeUnit.Seconds };
public void TrackRequest() { _metrics.Measure.Counter.Increment(_requestCounter); }
public IDisposable MeasureRequestDuration() { return _metrics.Measure.Timer.Time(_requestTimer); }}Use metrics in controllers:
[ApiController][Route("api/[controller]")]public class SampleController : ControllerBase{ private readonly MyMetrics _myMetrics;
public SampleController(MyMetrics myMetrics) { _myMetrics = myMetrics; }
[HttpGet] public IActionResult Get() { _myMetrics.TrackRequest();
using (_myMetrics.MeasureRequestDuration()) { // Simulating work Thread.Sleep(100); }
return Ok("Metrics tracked"); }}Health Checks
Also in library
Working with CSV in PHP: reading, writing, and data processing
GraphQL on Kotlin: Overview of Libraries and Tools for Server-Side Development
Logging for Swift (iOS/macOS): Best Libraries and Solutions
OAuth client for Rust: oauth2-rs library