Table of Contents

ServerTimingAttribute Class

Definition

Represents an attribute that is used to mark an action method for time measure profiling.

public class ServerTimingAttribute : ActionFilterAttribute, IActionFilter, IAsyncActionFilter, IResultFilter, IAsyncResultFilter, IOrderedFilter, IFilterFactory, IFilterMetadata
Inheritance
ServerTimingAttribute
Implements
Inherited Members

Examples

The following example applies to a controller action and configures the attribute directly. It instantiates the attribute with a name, description, threshold in milliseconds, and desired log level, then applies it declaratively to a WeatherController GET endpoint. The attribute outputs its configuration and confirms it implements IFilterFactory, demonstrating how to instrument ASP.NET Core endpoints with server-timing metrics.

using System;
using System.Threading.Tasks;
using Cuemon;
using Cuemon.AspNetCore.Mvc.Filters.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;

namespace MyApp.Examples;

public static class ServerTimingAttributeExample
{
    public static void Demonstrate()
    {
        var attribute = new ServerTimingAttribute
        {
            Name = "weatherApi",
            Description = "Weather API endpoint",
            Threshold = 500,
            ThresholdTimeUnit = TimeUnit.Milliseconds,
            DesiredLogLevel = LogLevel.Warning,
            EnvironmentName = string.Empty
        };

        Console.WriteLine(attribute.Name);
        Console.WriteLine(attribute is IFilterFactory);
        Console.WriteLine(attribute.IsReusable);
    }
}

[ApiController]
[Route("weather")]
public sealed class WeatherController : ControllerBase
{
    [HttpGet]
    [ServerTiming(
        Name = "weatherApi",
        Description = "Weather API endpoint",
        Threshold = 500,
        ThresholdTimeUnit = TimeUnit.Milliseconds,
        DesiredLogLevel = LogLevel.Warning)]
    public async Task<IActionResult> GetWeatherAsync()
    {
        await Task.Delay(300);
        return Ok(new { Temperature = 22, Condition = "Sunny" });
    }
}

Constructors

Name Description
ServerTimingAttribute()

Initializes a new instance of the ServerTimingAttribute class.

Properties

Name Description
Description

Gets or sets the server-specified metric description. Defaults the request URI of the action method.

DesiredLogLevel

Gets or sets the LogLevel of server-timing metrics. Defaults to Debug, which means logs are written with a severity level of debug.

EnvironmentName

Gets or sets the name of the environment to suppress the Server-Timing header from. Default is "Production".

IsReusable

Gets a value that indicates if the result of CreateInstance(IServiceProvider) can be reused across requests.

Name

Gets or sets the server-specified metric name. Defaults to the name of the action method.

Threshold

Gets or sets the value that in combination with ThresholdTimeUnit specifies the threshold of the action method.

ThresholdTimeUnit

Gets or sets one of the enumeration values that specifies the time unit of Threshold.

Methods

Name Description
CreateInstance(IServiceProvider)

Creates an instance of the executable filter.

See Also