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 requires Cuemon.AspNetCore.Mvc and sets up a ServiceCollection with logging, IHostEnvironment, and IServerTiming. It applies to WeatherController.GetWeatherAsync, creates the filter via CreateInstance, and executes OnActionExecuting and OnActionExecuted against an MVC action context. As the outcome, the response emits a Server-Timing header containing the weatherApi metric.

// Program.cs
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Cuemon;
using Cuemon.AspNetCore.Diagnostics;
using Cuemon.AspNetCore.Mvc.Filters.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MyApp.Examples;

await ServerTimingAttributeExample.DemonstrateAsync();

namespace MyApp.Examples
{
    public static class ServerTimingAttributeExample
    {
        public static async Task DemonstrateAsync()
        {
            var services = new ServiceCollection();
            services.AddLogging();
            services.AddSingleton<IHostEnvironment, SampleHostEnvironment>();
            services.AddSingleton<IServerTiming, ServerTiming>();
            using var provider = services.BuildServiceProvider();

            var attribute = new ServerTimingAttribute
            {
                Name = "weatherApi",
                Description = "Weather API endpoint",
                Threshold = 500,
                ThresholdTimeUnit = TimeUnit.Milliseconds,
                DesiredLogLevel = LogLevel.Warning,
                EnvironmentName = string.Empty
            };

            var filter = (ServerTimingFilter)attribute.CreateInstance(provider);

            var httpContext = new DefaultHttpContext { RequestServices = provider };
            var actionDescriptor = new ControllerActionDescriptor
            {
                ControllerTypeInfo = typeof(WeatherController).GetTypeInfo(),
                MethodInfo = typeof(WeatherController).GetMethod(nameof(WeatherController.GetWeatherAsync))!
            };
            var actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor);
            var filters = new List<IFilterMetadata> { filter };

            var executingContext = new ActionExecutingContext(actionContext, filters, new Dictionary<string, object?>(), new WeatherController());
            filter.OnActionExecuting(executingContext);
            await Task.Delay(25);
            var executedContext = new ActionExecutedContext(actionContext, filters, executingContext.Controller);
            filter.OnActionExecuted(executedContext);

            Console.WriteLine($"Attribute Name: {attribute.Name}");
            Console.WriteLine($"IsReusable: {attribute.IsReusable}");
            Console.WriteLine($"{ServerTiming.HeaderName}: {httpContext.Response.Headers[ServerTiming.HeaderName]}");
        }

        private sealed class SampleHostEnvironment : IHostEnvironment
        {
            public string ApplicationName { get; set; } = nameof(ServerTimingAttributeExample);
            public IFileProvider ContentRootFileProvider { get; set; } = new NullFileProvider();
            public string ContentRootPath { get; set; } = AppContext.BaseDirectory;
            public string EnvironmentName { get; set; } = Environments.Development;
        }
    }

    [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