Add project files.

This commit is contained in:
2024-08-16 11:24:48 +02:00
parent 6e24754b23
commit 6e4501f373
22 changed files with 682 additions and 0 deletions
@@ -0,0 +1,34 @@
using AspNetCore.Extensions.Options;
using Microsoft.CorrelationVector;
using Microsoft.Extensions.Options;
using System.Core.Extensions;
namespace AspNetCore.Extensions.Middleware;
public sealed class CorrelationVectorMiddleware : IMiddleware
{
private readonly CorrelationVectorOptions options;
public CorrelationVectorMiddleware() : this(Microsoft.Extensions.Options.Options.Create<CorrelationVectorOptions>(new()))
{
}
public CorrelationVectorMiddleware(IOptions<CorrelationVectorOptions> options)
{
this.options = options.ThrowIfNull().Value.ThrowIfNull();
}
public Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var cv = new CorrelationVector();
if (context.Items.TryGetValue(this.options.Header, out var correlationVectorVal) &&
correlationVectorVal is string correlationVectorStr)
{
cv = CorrelationVector.Parse(correlationVectorStr);
cv.Increment();
}
context.SetCorrelationVector(cv);
return next(context);
}
}
@@ -0,0 +1,22 @@
using System.Core.Extensions;
using System.Extensions;
namespace AspNetCore.Extensions.Middleware;
public sealed class HeaderLoggingMiddleware : IMiddleware
{
private readonly ILogger<HeaderLoggingMiddleware> logger;
public HeaderLoggingMiddleware(
ILogger<HeaderLoggingMiddleware> logger)
{
this.logger = logger.ThrowIfNull();
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var scopedLogger = this.logger.CreateScopedLogger(nameof(this.InvokeAsync), string.Empty);
scopedLogger.LogDebug(string.Join("\n", context.Request.Headers.Select(kvp => $"{kvp.Key}: {string.Join(",", kvp.Value.OfType<string>())}")));
await next(context);
}
}