Refactor CorrelationId

pull/847/head
Halil ibrahim Kalkan 6 years ago
parent bcb1aefb19
commit b1d9a1a66a

@ -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");
}
}
}

@ -9,6 +9,7 @@ using Volo.Abp.Clients;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.Timing; using Volo.Abp.Timing;
using Volo.Abp.Tracing;
using Volo.Abp.Users; using Volo.Abp.Users;
namespace Volo.Abp.Auditing namespace Volo.Abp.Auditing

@ -1,4 +1,4 @@
namespace Volo.Abp.Auditing namespace Volo.Abp.Tracing
{ {
public interface ICorrelationIdProvider public interface ICorrelationIdProvider
{ {

@ -1,6 +1,6 @@
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Auditing namespace Volo.Abp.Tracing
{ {
public class NullCorrelationIdProvider : ICorrelationIdProvider, ISingletonDependency public class NullCorrelationIdProvider : ICorrelationIdProvider, ISingletonDependency
{ {
Loading…
Cancel
Save