using Microsoft.Extensions.Primitives;
namespace ZB.MOM.WW.MxGateway.Server.Diagnostics;
/// Middleware extensions for structured gateway request logging with correlation context.
public static class GatewayRequestLoggingMiddlewareExtensions
{
/// Header name for the session ID.
public const string SessionIdHeaderName = "x-session-id";
/// Header name for the worker process ID.
public const string WorkerProcessIdHeaderName = "x-worker-process-id";
/// Header name for the correlation ID.
public const string CorrelationIdHeaderName = "x-correlation-id";
/// Header name for the command method name.
public const string CommandMethodHeaderName = "x-command-method";
/// Adds gateway request logging scope middleware that reads correlation headers and redacts sensitive data.
/// Application builder.
public static IApplicationBuilder UseGatewayRequestLoggingScope(this IApplicationBuilder app)
{
ArgumentNullException.ThrowIfNull(app);
return app.Use(async (context, next) =>
{
ILogger logger = context.RequestServices
.GetRequiredService()
.CreateLogger("MxGateway.Request");
using IDisposable? scope = logger.BeginGatewayScope(new GatewayLogScope(
SessionId: ReadHeader(context, SessionIdHeaderName),
WorkerProcessId: ReadInt32Header(context, WorkerProcessIdHeaderName),
CorrelationId: ReadUInt64Header(context, CorrelationIdHeaderName),
CommandMethod: ReadHeader(context, CommandMethodHeaderName),
ClientIdentity: ReadHeader(context, "authorization")));
await next(context);
});
}
private static string? ReadHeader(HttpContext context, string headerName)
{
return context.Request.Headers.TryGetValue(headerName, out StringValues values)
? values.ToString()
: null;
}
private static int? ReadInt32Header(HttpContext context, string headerName)
{
string? value = ReadHeader(context, headerName);
return int.TryParse(value, out int parsedValue)
? parsedValue
: null;
}
private static ulong? ReadUInt64Header(HttpContext context, string headerName)
{
string? value = ReadHeader(context, headerName);
return ulong.TryParse(value, out ulong parsedValue)
? parsedValue
: null;
}
}