Browse Source
Merge pull request #17224 from abpframework/maliming-patch-1
Update AbpCorrelationIdMiddleware.cs
pull/17234/head
liangshiwei
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
19 additions and
3 deletions
-
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs
|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using Microsoft.AspNetCore.Http; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using System.Threading.Tasks; |
|
|
|
@ -21,9 +22,24 @@ public class AbpCorrelationIdMiddleware : IMiddleware, ITransientDependency |
|
|
|
|
|
|
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next) |
|
|
|
{ |
|
|
|
var correlationId = _correlationIdProvider.Get(); |
|
|
|
CheckAndSetCorrelationIdOnResponse(context, _options, correlationId); |
|
|
|
await next(context); |
|
|
|
var correlationId = GetCorrelationIdFromRequest(context); |
|
|
|
using (_correlationIdProvider.Change(correlationId)) |
|
|
|
{ |
|
|
|
CheckAndSetCorrelationIdOnResponse(context, _options, correlationId); |
|
|
|
await next(context); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual string? GetCorrelationIdFromRequest(HttpContext context) |
|
|
|
{ |
|
|
|
var correlationId = context.Request.Headers[_options.HttpHeaderName]; |
|
|
|
if (correlationId.IsNullOrEmpty()) |
|
|
|
{ |
|
|
|
correlationId = Guid.NewGuid().ToString("N"); |
|
|
|
context.Request.Headers[_options.HttpHeaderName] = correlationId; |
|
|
|
} |
|
|
|
|
|
|
|
return correlationId; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual void CheckAndSetCorrelationIdOnResponse( |
|
|
|
|