All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 24s
Docker Build & Deploy / Deploy to Production (push) Successful in 6s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 3s
38 lines
906 B
C#
38 lines
906 B
C#
using Serilog.Context;
|
|
|
|
namespace WebApi.Middleware;
|
|
|
|
public class RequestIdMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public RequestIdMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext context)
|
|
{
|
|
var requestId = context.Request.Headers["X-Request-ID"].FirstOrDefault() ?? Guid.NewGuid().ToString("N");
|
|
|
|
context.Items["RequestId"] = requestId;
|
|
|
|
using (LogContext.PushProperty("RequestId", requestId))
|
|
{
|
|
await _next(context);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class RequestIdExtensions
|
|
{
|
|
public static string? GetRequestId(this HttpContext context)
|
|
{
|
|
return context.Items["RequestId"] as string;
|
|
}
|
|
|
|
public static IApplicationBuilder UseRequestId(this IApplicationBuilder builder)
|
|
{
|
|
return builder.UseMiddleware<RequestIdMiddleware>();
|
|
}
|
|
} |