Class ServerTimingAttribute
- Namespace
- Cuemon.AspNetCore.Mvc.Filters.Diagnostics
- Assembly
- Cuemon.AspNetCore.Mvc.dll
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
ServerTimingAttribute()
Initializes a new instance of the ServerTimingAttribute class.
public ServerTimingAttribute()
Properties
Description
Gets or sets the server-specified metric description. Defaults the request URI of the action method.
public string Description { get; set; }
Property Value
- string
The server-specified metric description.
DesiredLogLevel
Gets or sets the LogLevel of server-timing metrics. Defaults to Debug, which means logs are written with a severity level of debug.
public LogLevel DesiredLogLevel { get; set; }
Property Value
EnvironmentName
Gets or sets the name of the environment to suppress the Server-Timing header from. Default is "Production".
public string EnvironmentName { get; set; }
Property Value
- string
The name of the environment to suppress the Server-Timing header from.
Remarks
To always include the Server-Timing header, set this property to null or an empty string.
IsReusable
Gets a value that indicates if the result of CreateInstance(IServiceProvider) can be reused across requests.
public bool IsReusable { get; }
Property Value
- bool
trueif this instance is reusable; otherwise,false.
Name
Gets or sets the server-specified metric name. Defaults to the name of the action method.
public string Name { get; set; }
Property Value
- string
The server-specified metric name.
Threshold
Gets or sets the value that in combination with ThresholdTimeUnit specifies the threshold of the action method.
public double Threshold { get; set; }
Property Value
- double
The threshold value of the action method.
ThresholdTimeUnit
Gets or sets one of the enumeration values that specifies the time unit of Threshold.
public TimeUnit ThresholdTimeUnit { get; set; }
Property Value
Methods
CreateInstance(IServiceProvider)
Creates an instance of the executable filter.
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
Parameters
serviceProviderIServiceProviderThe request IServiceProvider.
Returns
- IFilterMetadata
An instance of the executable filter.