ServiceLevelIndicators.Asp 10.0.0-preview.2

This is a prerelease version of ServiceLevelIndicators.Asp.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package ServiceLevelIndicators.Asp --version 10.0.0-preview.2
                    
NuGet\Install-Package ServiceLevelIndicators.Asp -Version 10.0.0-preview.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ServiceLevelIndicators.Asp" Version="10.0.0-preview.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ServiceLevelIndicators.Asp" Version="10.0.0-preview.2" />
                    
Directory.Packages.props
<PackageReference Include="ServiceLevelIndicators.Asp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ServiceLevelIndicators.Asp --version 10.0.0-preview.2
                    
#r "nuget: ServiceLevelIndicators.Asp, 10.0.0-preview.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ServiceLevelIndicators.Asp@10.0.0-preview.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ServiceLevelIndicators.Asp&version=10.0.0-preview.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ServiceLevelIndicators.Asp&version=10.0.0-preview.2&prerelease
                    
Install as a Cake Tool

ServiceLevelIndicators.Asp

NuGet Package

ASP.NET Core middleware that automatically emits Service Level Indicator (SLI) latency metrics for every API operation. Built on the ServiceLevelIndicators core library and the standard System.Diagnostics.Metrics API.

For API versioning support, add ServiceLevelIndicators.Asp.ApiVersioning.

Installation

dotnet add package ServiceLevelIndicators.Asp

Quick Start — MVC Controllers

// 1. Register with OpenTelemetry
builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics =>
    {
        metrics.AddServiceLevelIndicatorInstrumentation();
        metrics.AddOtlpExporter();
    });

// 2. Configure SLI — AddMvc() enables attribute-based overrides
builder.Services.AddServiceLevelIndicator(options =>
{
    options.LocationId = ServiceLevelIndicator.CreateLocationId("public", "westus3");
})
.AddMvc();

// 3. Add the middleware
app.UseServiceLevelIndicator();

Quick Start — Minimal APIs

builder.Services.AddServiceLevelIndicator(options =>
{
    options.LocationId = ServiceLevelIndicator.CreateLocationId("public", "westus3");
});

app.UseServiceLevelIndicator();

// Mark individual endpoints
app.MapGet("/hello", () => "Hello World!")
    .AddServiceLevelIndicator();

Emitted Metrics

A meter named ServiceLevelIndicator with instrument operation.duration (milliseconds) is emitted with the following attributes:

Attribute Description
Operation Defaults to the route template, e.g. GET WeatherForecast
CustomerResourceId Identifies the customer or caller
LocationId Where the service is running
activity.status.code Ok (2xx), Error (5xx), or Unset (other)
http.response.status.code The HTTP response status code
http.request.method (Optional) The HTTP method — enabled via AddHttpMethod()

Customizations

Add HTTP method as a dimension

builder.Services.AddServiceLevelIndicator(options => { /* ... */ })
    .AddMvc()
    .AddHttpMethod();

Enrich with custom data

Use Enrich (or EnrichAsync) to set CustomerResourceId or add custom attributes:

builder.Services.AddServiceLevelIndicator(options => { /* ... */ })
    .AddMvc()
    .Enrich(context =>
    {
        var upn = context.HttpContext.User.Claims
            .FirstOrDefault(c => c.Type == "upn")?.Value ?? "Unknown";
        context.SetCustomerResourceId(upn);
        context.AddAttribute("UserPrincipalName", upn);
    });

Override the operation name

[HttpGet("MyAction")]
[ServiceLevelIndicator(Operation = "MyCustomName")]
public IActionResult Get() => Ok();

Set CustomerResourceId from a route parameter

[HttpGet("get-by-zip-code/{zipCode}")]
public IActionResult GetByZipcode([CustomerResourceId] string zipCode) => Ok();

Or imperatively:

HttpContext.GetMeasuredOperation().CustomerResourceId = customerResourceId;

Add custom attributes from route parameters

Parameters decorated with [Measure] are automatically added as metric dimensions:

[HttpGet("name/{first}/{surname}")]
public IActionResult Get([Measure] string first, [CustomerResourceId] string surname) => Ok();

Add custom attributes manually

HttpContext.GetMeasuredOperation().AddAttribute("CustomKey", value);

// Safe version for middleware (won't throw if SLI is not configured for the route)
if (HttpContext.TryGetMeasuredOperation(out var op))
    op.AddAttribute("CustomKey", value);

Opt-in mode

To disable automatic SLI emission on all controllers:

options.AutomaticallyEmitted = false;

Then add [ServiceLevelIndicator] only to the controllers you want measured.

Further Reading

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ServiceLevelIndicators.Asp:

Package Downloads
ServiceLevelIndicators.Asp.ApiVersioning

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.0-preview.6 44 3/14/2026
10.0.0-preview.2 67 3/5/2026
8.0.1 347 2/11/2025
8.0.0-alpha.25 123 11/22/2024
8.0.0-alpha.24 100 11/20/2024
8.0.0-alpha.17 161 5/21/2024
8.0.0-alpha.12 167 3/29/2024
8.0.0-alpha.7 131 1/24/2024
8.0.0-alpha.6 133 1/23/2024
1.1.2 341 12/14/2023
1.1.1 301 11/10/2023
1.0.1 304 10/3/2023
0.1.0-alpha.20 172 9/27/2023
0.1.0-alpha.19 134 9/27/2023
0.1.0-alpha.18 195 9/14/2023
0.1.0-alpha.10 235 8/23/2023