#2597 Refactor AbpAspNetCoreSerilogOptions

pull/2661/head
Halil İbrahim Kalkan 5 years ago
parent da4c91cabd
commit c88cd2054e

@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.Builder
public static IApplicationBuilder UseSerilogEnrichers(this IApplicationBuilder app)
{
return app
.UseMiddleware<SerilogMiddleware>();
.UseMiddleware<AbpSerilogMiddleware>();
}
}
}

@ -1,21 +0,0 @@
namespace Volo.Abp.AspNetCore.Serilog
{
public class AbpAspNetCoreSerilogEnrichersOptions
{
public string TenantIdEnricherPropertyName { get; set; }
public string UserIdEnricherPropertyName { get; set; }
public string ClientIdEnricherPropertyName { get; set; }
public string CorrelationIdPropertyName { get; set; }
public AbpAspNetCoreSerilogEnrichersOptions()
{
TenantIdEnricherPropertyName = AbpSerilogEnrichersConsts.TenantIdEnricherPropertyName;
UserIdEnricherPropertyName = AbpSerilogEnrichersConsts.UserIdEnricherPropertyName;
ClientIdEnricherPropertyName = AbpSerilogEnrichersConsts.ClientIdEnricherPropertyName;
CorrelationIdPropertyName = AbpSerilogEnrichersConsts.CorrelationIdPropertyName;
}
}
}

@ -0,0 +1,30 @@
namespace Volo.Abp.AspNetCore.Serilog
{
public class AbpAspNetCoreSerilogOptions
{
public AllEnricherPropertyNames EnricherPropertyNames { get; } = new AllEnricherPropertyNames();
public class AllEnricherPropertyNames
{
/// <summary>
/// Default value: "TenantId".
/// </summary>
public string TenantId { get; set; } = "TenantId";
/// <summary>
/// Default value: "UserId".
/// </summary>
public string UserId { get; set; } = "UserId";
/// <summary>
/// Default value: "ClientId".
/// </summary>
public string ClientId { get; set; } = "ClientId";
/// <summary>
/// Default value: "CorrelationId".
/// </summary>
public string CorrelationId { get; set; } = "CorrelationId";
}
}
}

@ -1,10 +0,0 @@
namespace Volo.Abp.AspNetCore.Serilog
{
public class AbpSerilogEnrichersConsts
{
public const string TenantIdEnricherPropertyName = "TenantId";
public const string UserIdEnricherPropertyName = "UserId";
public const string ClientIdEnricherPropertyName = "ClientId";
public const string CorrelationIdPropertyName = "CorrelationId";
}
}

@ -13,20 +13,20 @@ using Volo.Abp.Users;
namespace Volo.Abp.AspNetCore.Serilog
{
public class SerilogMiddleware : IMiddleware, ITransientDependency
public class AbpSerilogMiddleware : IMiddleware, ITransientDependency
{
private readonly ICurrentClient _currentClient;
private readonly ICurrentTenant _currentTenant;
private readonly ICurrentUser _currentUser;
private readonly ICorrelationIdProvider _correlationIdProvider;
private readonly AbpAspNetCoreSerilogEnrichersOptions _options;
private readonly AbpAspNetCoreSerilogOptions _options;
public SerilogMiddleware(
public AbpSerilogMiddleware(
ICurrentTenant currentTenant,
ICurrentUser currentUser,
ICurrentClient currentClient,
ICorrelationIdProvider correlationIdProvider,
IOptions<AbpAspNetCoreSerilogEnrichersOptions> options)
IOptions<AbpAspNetCoreSerilogOptions> options)
{
_currentTenant = currentTenant;
_currentUser = currentUser;
@ -41,23 +41,23 @@ namespace Volo.Abp.AspNetCore.Serilog
if (_currentTenant?.Id != null)
{
enrichers.Add(new PropertyEnricher(_options.TenantIdEnricherPropertyName, _currentTenant.Id));
enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.TenantId, _currentTenant.Id));
}
if (_currentUser?.Id != null)
{
enrichers.Add(new PropertyEnricher(_options.UserIdEnricherPropertyName, _currentUser.Id));
enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.UserId, _currentUser.Id));
}
if (_currentClient?.Id != null)
{
enrichers.Add(new PropertyEnricher(_options.ClientIdEnricherPropertyName, _currentClient.Id));
enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.ClientId, _currentClient.Id));
}
var correlationId = _correlationIdProvider.Get();
if (!string.IsNullOrEmpty(correlationId))
{
enrichers.Add(new PropertyEnricher(_options.CorrelationIdPropertyName, correlationId));
enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.CorrelationId, correlationId));
}
using (LogContext.Push(enrichers.ToArray()))

@ -22,14 +22,14 @@ namespace Volo.Abp.AspNetCore.Serilog
private readonly string _testTenantName = "acme";
private readonly AbpAspNetCoreMultiTenancyOptions _tenancyOptions;
private readonly AbpAspNetCoreSerilogEnrichersOptions _serilogEnrichersOptions;
private readonly AbpAspNetCoreSerilogOptions _serilogOptions;
private readonly ILogger<Serilog_Enrichers_Tests> _logger;
public Serilog_Enrichers_Tests()
{
_tenancyOptions = ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreMultiTenancyOptions>>().Value;
_serilogEnrichersOptions =
ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreSerilogEnrichersOptions>>().Value;
_serilogOptions =
ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreSerilogOptions>>().Value;
_logger = ServiceProvider.GetRequiredService<ILogger<Serilog_Enrichers_Tests>>();
}
@ -56,7 +56,7 @@ namespace Volo.Abp.AspNetCore.Serilog
var executedLogEvent = GetLogEvent(ExecutedEndpointLogEventText);
executedLogEvent.ShouldNotBeNull();
executedLogEvent.Properties.ContainsKey(_serilogEnrichersOptions.TenantIdEnricherPropertyName)
executedLogEvent.Properties.ContainsKey(_serilogOptions.EnricherPropertyNames.TenantId)
.ShouldBe(false);
}
@ -71,9 +71,9 @@ namespace Volo.Abp.AspNetCore.Serilog
var executedLogEvent = GetLogEvent(ExecutedEndpointLogEventText);
executedLogEvent.ShouldNotBeNull();
executedLogEvent.Properties.ContainsKey(_serilogEnrichersOptions.TenantIdEnricherPropertyName)
executedLogEvent.Properties.ContainsKey(_serilogOptions.EnricherPropertyNames.TenantId)
.ShouldBe(true);
((ScalarValue) executedLogEvent.Properties[_serilogEnrichersOptions.TenantIdEnricherPropertyName]).Value
((ScalarValue) executedLogEvent.Properties[_serilogOptions.EnricherPropertyNames.TenantId]).Value
.ShouldBe(_testTenantId);
}
@ -87,10 +87,10 @@ namespace Volo.Abp.AspNetCore.Serilog
executedLogEvent.ShouldNotBeNull();
executedLogEvent.Properties.ContainsKey(_serilogEnrichersOptions.CorrelationIdPropertyName)
executedLogEvent.Properties.ContainsKey(_serilogOptions.EnricherPropertyNames.CorrelationId)
.ShouldNotBeNull();
((ScalarValue) executedLogEvent.Properties[_serilogEnrichersOptions.CorrelationIdPropertyName]).Value
((ScalarValue) executedLogEvent.Properties[_serilogOptions.EnricherPropertyNames.CorrelationId]).Value
.ShouldBe(result);
}
}

Loading…
Cancel
Save