From 9359d8ac74fd9080450d859343747b75e4aef500 Mon Sep 17 00:00:00 2001 From: David Acker Date: Sat, 17 Jun 2023 12:14:24 -0400 Subject: [PATCH] Add sample for IHttpSysRequestTimingFeature --- .../IHttpSysRequestTimingFeature.csproj | 10 ++ .../IHttpSysRequestTimingFeature/Program.cs | 110 ++++++++++++++++++ .../request-features/samples/removeMe.txt | 1 - 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 aspnetcore/fundamentals/request-features/samples/8.x/IHttpSysRequestTimingFeature/IHttpSysRequestTimingFeature.csproj create mode 100644 aspnetcore/fundamentals/request-features/samples/8.x/IHttpSysRequestTimingFeature/Program.cs delete mode 100644 aspnetcore/fundamentals/request-features/samples/removeMe.txt diff --git a/aspnetcore/fundamentals/request-features/samples/8.x/IHttpSysRequestTimingFeature/IHttpSysRequestTimingFeature.csproj b/aspnetcore/fundamentals/request-features/samples/8.x/IHttpSysRequestTimingFeature/IHttpSysRequestTimingFeature.csproj new file mode 100644 index 0000000000..8116bc0363 --- /dev/null +++ b/aspnetcore/fundamentals/request-features/samples/8.x/IHttpSysRequestTimingFeature/IHttpSysRequestTimingFeature.csproj @@ -0,0 +1,10 @@ + + + + net8.0-windows + enable + enable + true + + + diff --git a/aspnetcore/fundamentals/request-features/samples/8.x/IHttpSysRequestTimingFeature/Program.cs b/aspnetcore/fundamentals/request-features/samples/8.x/IHttpSysRequestTimingFeature/Program.cs new file mode 100644 index 0000000000..86e399a6de --- /dev/null +++ b/aspnetcore/fundamentals/request-features/samples/8.x/IHttpSysRequestTimingFeature/Program.cs @@ -0,0 +1,110 @@ +#define WithTimestamps // WithTimestamps, WithTryGetTimestamp, WithTryGetElapsedTime + +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Server.HttpSys; + +#if WithTimestamps +#region snippet_WithTimestamps +var builder = WebApplication.CreateBuilder(args); + +builder.WebHost.UseHttpSys(); + +var app = builder.Build(); + +app.Use((context, next) => +{ + var feature = context.Features.GetRequiredFeature(); + + var loggerFactory = context.RequestServices.GetRequiredService(); + var logger = loggerFactory.CreateLogger("Sample"); + + var timestamps = feature.Timestamps; + + for (var i = 0; i < timestamps.Length; i++) + { + var timestamp = timestamps[i]; + var timingType = (HttpSysRequestTimingType)i; + + logger.LogInformation("Timestamp {timingType}: {timestamp}", timingType, timestamp); + } + + return next(context); +}); + +app.MapGet("/", () => Results.Ok()); + +app.Run(); +#endregion +#elif WithTryGetTimestamp +#region snippet_WithTryGetTimestamp +var builder = WebApplication.CreateBuilder(args); + +builder.WebHost.UseHttpSys(); + +var app = builder.Build(); + +app.Use((context, next) => +{ + var feature = context.Features.GetRequiredFeature(); + + var loggerFactory = context.RequestServices.GetRequiredService(); + var logger = loggerFactory.CreateLogger("Sample"); + + var timingType = HttpSysRequestTimingType.RequestRoutingEnd; + + if (feature.TryGetTimestamp(timingType, out var timestamp)) + { + logger.LogInformation("Timestamp {timingType}: {timestamp}", timingType, timestamp); + } + else + { + logger.LogInformation("Timestamp {timingType}: not available for the current request", timingType); + } + + return next(context); +}); + +app.MapGet("/", () => Results.Ok()); + +app.Run(); +#endregion +#elif WithTryGetElapsedTime +#region snippet_WithTryGetElapsedTime +var builder = WebApplication.CreateBuilder(args); + +builder.WebHost.UseHttpSys(); + +var app = builder.Build(); + +app.Use((context, next) => +{ + var feature = context.Features.GetRequiredFeature(); + + var loggerFactory = context.RequestServices.GetRequiredService(); + var logger = loggerFactory.CreateLogger("Sample"); + + var startingTimingType = HttpSysRequestTimingType.RequestRoutingStart; + var endingTimingType = HttpSysRequestTimingType.RequestRoutingEnd; + + if (feature.TryGetElapsedTime(startingTimingType, endingTimingType, out var elapsed)) + { + logger.LogInformation("Elapsed time {startingTimingType} to {endingTimingType}: {elapsed}", + startingTimingType, + endingTimingType, + elapsed); + } + else + { + logger.LogInformation("Elapsed time {startingTimingType} to {endingTimingType}: not available for the current request.", + startingTimingType, + endingTimingType); + } + + return next(context); +}); + +app.MapGet("/", () => Results.Ok()); + +app.Run(); +#endregion +#endif diff --git a/aspnetcore/fundamentals/request-features/samples/removeMe.txt b/aspnetcore/fundamentals/request-features/samples/removeMe.txt deleted file mode 100644 index 897beaa0bc..0000000000 --- a/aspnetcore/fundamentals/request-features/samples/removeMe.txt +++ /dev/null @@ -1 +0,0 @@ -Remove after creating sample