Browse Source

Adding AbpAspNetCoreSerilogEnrichersOptions.

pull/2597/head
Mehmet Tüken 7 years ago
parent
commit
3c479b9524
  1. 21
      framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpAspNetCoreSerilogEnrichersOptions.cs
  2. 10
      framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogEnrichersConsts.cs
  3. 19
      framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/SerilogMiddleware.cs

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

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

19
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<AbpAspNetCoreSerilogEnrichersOptions> 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()))

Loading…
Cancel
Save