diff --git a/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogEnrichersOptions.cs b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogEnrichersOptions.cs new file mode 100644 index 0000000000..06453cb61f --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogEnrichersOptions.cs @@ -0,0 +1,21 @@ +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; + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogEnrichersConsts.cs b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogEnrichersConsts.cs new file mode 100644 index 0000000000..bf2832b553 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogEnrichersConsts.cs @@ -0,0 +1,10 @@ +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"; + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/SerilogMiddleware.cs b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/SerilogMiddleware.cs index 10bbdb188f..e03bcd5342 100644 --- a/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/SerilogMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/SerilogMiddleware.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Options; using Serilog.Context; using Serilog.Core; using Serilog.Core.Enrichers; @@ -14,26 +15,24 @@ namespace Volo.Abp.AspNetCore.Serilog { public class SerilogMiddleware : IMiddleware, ITransientDependency { - private const string TenantEnricherPropertyName = "TenantId"; - private const string UserEnricherPropertyName = "UserId"; - private const string ClientEnricherPropertyName = "ClientId"; - private const string CorrelationIdPropertyName = "CorrelationId"; - private readonly ICurrentClient _currentClient; private readonly ICurrentTenant _currentTenant; private readonly ICurrentUser _currentUser; private readonly ICorrelationIdProvider _correlationIdProvider; + private readonly AbpAspNetCoreSerilogEnrichersOptions _options; public SerilogMiddleware( ICurrentTenant currentTenant, ICurrentUser currentUser, ICurrentClient currentClient, - ICorrelationIdProvider correlationIdProvider) + ICorrelationIdProvider correlationIdProvider, + IOptions options) { _currentTenant = currentTenant; _currentUser = currentUser; _currentClient = currentClient; _correlationIdProvider = correlationIdProvider; + _options = options.Value; } public async Task InvokeAsync(HttpContext context, RequestDelegate next) @@ -42,23 +41,23 @@ namespace Volo.Abp.AspNetCore.Serilog if (_currentTenant?.Id != null) { - enrichers.Add(new PropertyEnricher(TenantEnricherPropertyName, _currentTenant.Id)); + enrichers.Add(new PropertyEnricher(_options.TenantIdEnricherPropertyName, _currentTenant.Id)); } if (_currentUser?.Id != null) { - enrichers.Add(new PropertyEnricher(UserEnricherPropertyName, _currentUser.Id)); + enrichers.Add(new PropertyEnricher(_options.UserIdEnricherPropertyName, _currentUser.Id)); } if (_currentClient?.Id != null) { - enrichers.Add(new PropertyEnricher(ClientEnricherPropertyName, _currentClient.Id)); + enrichers.Add(new PropertyEnricher(_options.ClientIdEnricherPropertyName, _currentClient.Id)); } var correlationId = _correlationIdProvider.Get(); if (!string.IsNullOrEmpty(correlationId)) { - enrichers.Add(new PropertyEnricher(CorrelationIdPropertyName, correlationId)); + enrichers.Add(new PropertyEnricher(_options.CorrelationIdPropertyName, correlationId)); } using (LogContext.Push(enrichers.ToArray()))