Browse Source

#2597 Refactor AbpAspNetCoreSerilogOptions

pull/2661/head
Halil İbrahim Kalkan 7 years ago
parent
commit
c88cd2054e
  1. 2
      framework/src/Volo.Abp.AspNetCore.Serilog/Microsoft/AspNetCore/Builder/AbpAspNetCoreSerilogApplicationBuilderExtensions.cs
  2. 21
      framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogEnrichersOptions.cs
  3. 1
      framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogModule.cs
  4. 30
      framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogOptions.cs
  5. 10
      framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogEnrichersConsts.cs
  6. 16
      framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs
  7. 16
      framework/test/Volo.Abp.AspNetCore.Serilog.Tests/Volo/Abp/AspNetCore/Serilog/Serilog_Enrichers_Tests.cs

2
framework/src/Volo.Abp.AspNetCore.Serilog/Microsoft/AspNetCore/Builder/AbpAspNetCoreSerilogApplicationBuilderExtensions.cs

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

21
framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogEnrichersOptions.cs

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

1
framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogModule.cs → framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogModule.cs

@ -1,4 +1,3 @@
using Volo.Abp.AspNetCore;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;

30
framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogOptions.cs

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

10
framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogEnrichersConsts.cs

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

16
framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/SerilogMiddleware.cs → framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs

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

16
framework/test/Volo.Abp.AspNetCore.Serilog.Tests/Volo/Abp/AspNetCore/Serilog/Serilog_Enrichers_Tests.cs

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

Loading…
Cancel
Save