mirror of https://github.com/abpframework/abp.git
5 changed files with 132 additions and 1 deletions
@ -0,0 +1,13 @@ |
|||||
|
using Volo.Abp.AspNetCore.Serilog; |
||||
|
|
||||
|
namespace Microsoft.AspNetCore.Builder |
||||
|
{ |
||||
|
public static class AbpAspNetCoreSerilogApplicationBuilderExtensions |
||||
|
{ |
||||
|
public static IApplicationBuilder UseSerilogEnrichers(this IApplicationBuilder app) |
||||
|
{ |
||||
|
return app |
||||
|
.UseMiddleware<SerilogMiddleware>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk.Web"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp3.1</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.AspNetCore.Serilog</AssemblyName> |
||||
|
<PackageId>Volo.Abp.AspNetCore.Serilog</PackageId> |
||||
|
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<IsPackable>true</IsPackable> |
||||
|
<OutputType>Library</OutputType> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\Volo.Abp.AspNetCore\Volo.Abp.AspNetCore.csproj" /> |
||||
|
<ProjectReference Include="..\Volo.Abp.MultiTenancy\Volo.Abp.MultiTenancy.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Serilog" Version="2.8.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,14 @@ |
|||||
|
using Volo.Abp.AspNetCore; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Serilog |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpMultiTenancyModule), |
||||
|
typeof(AbpAspNetCoreModule) |
||||
|
)] |
||||
|
public class AbpSerilogModule : AbpModule |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,70 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Serilog.Context; |
||||
|
using Serilog.Core; |
||||
|
using Serilog.Core.Enrichers; |
||||
|
using Volo.Abp.Clients; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Volo.Abp.Tracing; |
||||
|
using Volo.Abp.Users; |
||||
|
|
||||
|
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; |
||||
|
|
||||
|
public SerilogMiddleware( |
||||
|
ICurrentTenant currentTenant, |
||||
|
ICurrentUser currentUser, |
||||
|
ICurrentClient currentClient, |
||||
|
ICorrelationIdProvider correlationIdProvider) |
||||
|
{ |
||||
|
_currentTenant = currentTenant; |
||||
|
_currentUser = currentUser; |
||||
|
_currentClient = currentClient; |
||||
|
_correlationIdProvider = correlationIdProvider; |
||||
|
} |
||||
|
|
||||
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next) |
||||
|
{ |
||||
|
var enrichers = new List<ILogEventEnricher>(); |
||||
|
|
||||
|
if (_currentTenant?.Id != null) |
||||
|
{ |
||||
|
enrichers.Add(new PropertyEnricher(TenantEnricherPropertyName, _currentTenant.Id)); |
||||
|
} |
||||
|
|
||||
|
if (_currentUser?.Id != null) |
||||
|
{ |
||||
|
enrichers.Add(new PropertyEnricher(UserEnricherPropertyName, _currentUser.Id)); |
||||
|
} |
||||
|
|
||||
|
if (_currentClient?.Id != null) |
||||
|
{ |
||||
|
enrichers.Add(new PropertyEnricher(ClientEnricherPropertyName, _currentClient.Id)); |
||||
|
} |
||||
|
|
||||
|
var correlationId = _correlationIdProvider.Get(); |
||||
|
if (!string.IsNullOrEmpty(correlationId)) |
||||
|
{ |
||||
|
enrichers.Add(new PropertyEnricher(CorrelationIdPropertyName, correlationId)); |
||||
|
} |
||||
|
|
||||
|
using (LogContext.Push(enrichers.ToArray())) |
||||
|
{ |
||||
|
await next(context).ConfigureAwait(false); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue