Skip to content

Commit

Permalink
#2597 Refactor AbpAspNetCoreSerilogOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Jan 13, 2020
1 parent da4c91c commit c88cd20
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class AbpAspNetCoreSerilogApplicationBuilderExtensions
public static IApplicationBuilder UseSerilogEnrichers(this IApplicationBuilder app)
{
return app
.UseMiddleware<SerilogMiddleware>();
.UseMiddleware<AbpSerilogMiddleware>();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Volo.Abp.AspNetCore;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;

Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@

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;
Expand All @@ -41,23 +41,23 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)

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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class Serilog_Enrichers_Tests : AbpSerilogTestBase
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>>();
}

Expand All @@ -56,7 +56,7 @@ public async Task TenantId_Not_Set_Test()
var executedLogEvent = GetLogEvent(ExecutedEndpointLogEventText);

executedLogEvent.ShouldNotBeNull();
executedLogEvent.Properties.ContainsKey(_serilogEnrichersOptions.TenantIdEnricherPropertyName)
executedLogEvent.Properties.ContainsKey(_serilogOptions.EnricherPropertyNames.TenantId)
.ShouldBe(false);
}

Expand All @@ -71,9 +71,9 @@ public async Task TenantId_Set_Test()
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);
}

Expand All @@ -87,10 +87,10 @@ public async Task CorrelationId_Enrichers_Test()

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);
}
}
Expand Down

0 comments on commit c88cd20

Please sign in to comment.