Spaces:
Runtime error
Runtime error
Download src/SCM.Api/Middleware/CorrelationIdMiddleware.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Api/Middleware/CorrelationIdMiddleware.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Api/Middleware/CorrelationIdMiddleware.cs
-
curl -L -o CorrelationIdMiddleware.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Api/Middleware/CorrelationIdMiddleware.cs
1.1 kB
| using Serilog.Context; | |
| namespace SCM.Api.Middleware; | |
| /// <summary>Garante um CorrelationId por requisição e o propaga para logs e resposta.</summary> | |
| public sealed class CorrelationIdMiddleware | |
| { | |
| public const string HeaderName = "X-Correlation-ID"; | |
| private readonly RequestDelegate _next; | |
| public CorrelationIdMiddleware(RequestDelegate next) => _next = next; | |
| public async Task InvokeAsync(HttpContext context) | |
| { | |
| var correlationId = context.Request.Headers.TryGetValue(HeaderName, out var value) | |
| && !string.IsNullOrWhiteSpace(value) | |
| ? value.ToString() | |
| : Guid.NewGuid().ToString("D"); | |
| context.Items[HeaderName] = correlationId; | |
| context.Response.OnStarting(() => | |
| { | |
| context.Response.Headers[HeaderName] = correlationId; | |
| return Task.CompletedTask; | |
| }); | |
| using (LogContext.PushProperty("CorrelationId", correlationId)) | |
| using (LogContext.PushProperty("RequestId", context.TraceIdentifier)) | |
| { | |
| await _next(context); | |
| } | |
| } | |
| } | |