mirror of https://github.com/abpframework/abp.git
6 changed files with 59 additions and 45 deletions
@ -1,43 +0,0 @@ |
|||
using System; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Volo.Abp.Auditing; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Auditing |
|||
{ |
|||
[Dependency(ReplaceServices = true)] |
|||
public class AspNetCoreCorrelationIdProvider : ICorrelationIdProvider, ITransientDependency |
|||
{ |
|||
public const string CorrelationIdKey = "_CorrelationId"; |
|||
|
|||
protected IHttpContextAccessor HttpContextAccessor { get; } |
|||
|
|||
public AspNetCoreCorrelationIdProvider(IHttpContextAccessor httpContextAccessor) |
|||
{ |
|||
HttpContextAccessor = httpContextAccessor; |
|||
} |
|||
|
|||
public virtual string Get() |
|||
{ |
|||
if (HttpContextAccessor.HttpContext?.Request?.Headers == null) |
|||
{ |
|||
return CreateNewCorrelationId(); |
|||
} |
|||
|
|||
string correlationId = HttpContextAccessor.HttpContext.Request.Headers[CorrelationIdKey]; |
|||
|
|||
if (correlationId.IsNullOrEmpty()) |
|||
{ |
|||
correlationId = CreateNewCorrelationId(); |
|||
HttpContextAccessor.HttpContext.Request.Headers[CorrelationIdKey] = correlationId; |
|||
} |
|||
|
|||
return correlationId; |
|||
} |
|||
|
|||
protected virtual string CreateNewCorrelationId() |
|||
{ |
|||
return Guid.NewGuid().ToString("N"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.AspNetCore.Tracing |
|||
{ |
|||
public class AspNetCoreCorrelationIdOptions |
|||
{ |
|||
public string HeaderName { get; set; } = "X-Correlation-Id"; |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
using System; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Tracing; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Tracing |
|||
{ |
|||
[Dependency(ReplaceServices = true)] |
|||
public class AspNetCoreCorrelationIdProvider : ICorrelationIdProvider, ITransientDependency |
|||
{ |
|||
protected IHttpContextAccessor HttpContextAccessor { get; } |
|||
protected AspNetCoreCorrelationIdOptions Options { get; } |
|||
|
|||
public AspNetCoreCorrelationIdProvider( |
|||
IHttpContextAccessor httpContextAccessor, |
|||
IOptions<AspNetCoreCorrelationIdOptions> options) |
|||
{ |
|||
HttpContextAccessor = httpContextAccessor; |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public virtual string Get() |
|||
{ |
|||
if (HttpContextAccessor.HttpContext?.Request?.Headers == null) |
|||
{ |
|||
return CreateNewCorrelationId(); |
|||
} |
|||
|
|||
lock (HttpContextAccessor.HttpContext.Request.Headers) |
|||
{ |
|||
string correlationId = HttpContextAccessor.HttpContext.Request.Headers[Options.HeaderName]; |
|||
|
|||
if (correlationId.IsNullOrEmpty()) |
|||
{ |
|||
correlationId = CreateNewCorrelationId(); |
|||
HttpContextAccessor.HttpContext.Request.Headers[Options.HeaderName] = correlationId; |
|||
} |
|||
|
|||
return correlationId; |
|||
} |
|||
} |
|||
|
|||
protected virtual string CreateNewCorrelationId() |
|||
{ |
|||
return Guid.NewGuid().ToString("N"); |
|||
} |
|||
} |
|||
} |
|||
@ -1,4 +1,4 @@ |
|||
namespace Volo.Abp.Auditing |
|||
namespace Volo.Abp.Tracing |
|||
{ |
|||
public interface ICorrelationIdProvider |
|||
{ |
|||
@ -1,6 +1,6 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Auditing |
|||
namespace Volo.Abp.Tracing |
|||
{ |
|||
public class NullCorrelationIdProvider : ICorrelationIdProvider, ISingletonDependency |
|||
{ |
|||
Loading…
Reference in new issue