Table of Contents

ServerTimingFilter Class

Definition

A filter that performs time measure profiling of action methods.

public class ServerTimingFilter : ConfigurableActionFilter<ServerTimingOptions>, IConfigurable<ServerTimingOptions>, IActionFilter, IFilterMetadata
Inheritance
ServerTimingFilter
Implements
Inherited Members

Examples

The following example requires Cuemon.AspNetCore.Mvc and sets up a ServiceCollection with IServerTiming, ServerTimingOptions (UseTimeMeasureProfiler = true), IHostEnvironment, and ILogger for . It prepares an MVC ActionExecutingContext and ActionExecutedContext, then invokes OnActionExecuting and OnActionExecuted around an asynchronous delay. As the outcome, the response emits a Server-Timing header with the recorded action duration.

// Program.cs
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
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 Microsoft.Extensions.Options;
using MyApp.Examples;

await ServerTimingFilterExample.DemonstrateAsync();

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

            var options = Options.Create(new ServerTimingOptions
            {
                UseTimeMeasureProfiler = true,
                SuppressHeaderPredicate = _ => false
            });

            using var loggerFactory = LoggerFactory.Create(_ => { });
            var filter = new ServerTimingFilter(
                options,
                new SampleHostEnvironment(),
                loggerFactory.CreateLogger<ServerTimingFilter>());

            var httpContext = new DefaultHttpContext { RequestServices = provider };
            httpContext.Request.Scheme = "https";
            httpContext.Request.Host = new HostString("api.example.com");
            httpContext.Request.Path = "/orders";

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

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

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

        public static void SampleAction()
        {
        }

        private sealed class SampleHostEnvironment : IHostEnvironment
        {
            public string EnvironmentName { get; set; } = Environments.Development;

            public string ApplicationName { get; set; } = nameof(ServerTimingFilterExample);

            public string ContentRootPath { get; set; } = AppContext.BaseDirectory;

            public IFileProvider ContentRootFileProvider { get; set; } = new NullFileProvider();
        }
    }
}

Constructors

Name Description
ServerTimingFilter(IOptions<ServerTimingOptions>, IHostEnvironment, ILogger<ServerTimingFilter>)

Initializes a new instance of the ServerTimingFilter class.

Methods

Name Description
OnActionExecuted(ActionExecutedContext)

Called after the action executes, before the action result.

OnActionExecuting(ActionExecutingContext)

Called before the action executes, after model binding is complete.

See Also