Browse Source
Merge pull request #11797 from abpframework/AlwaysLogSelectors
Add `AlwaysLogSelectors` to `AuditingOptions`
pull/11819/head
liangshiwei
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with
55 additions and
6 deletions
-
framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs
-
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs
-
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs
-
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs
|
|
|
@ -50,7 +50,7 @@ public class AbpAuditHubFilter : IHubFilter |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
if (ShouldWriteAuditLog(invocationContext.ServiceProvider, hasError)) |
|
|
|
if (await ShouldWriteAuditLogAsync(auditingManager.Current.Log, invocationContext.ServiceProvider, hasError)) |
|
|
|
{ |
|
|
|
var unitOfWorkManager = invocationContext.ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); |
|
|
|
if (unitOfWorkManager.Current != null) |
|
|
|
@ -76,9 +76,18 @@ public class AbpAuditHubFilter : IHubFilter |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private bool ShouldWriteAuditLog(IServiceProvider serviceProvider, bool hasError) |
|
|
|
private async Task<bool> ShouldWriteAuditLogAsync(AuditLogInfo auditLogInfo, IServiceProvider serviceProvider, bool hasError) |
|
|
|
{ |
|
|
|
var options = serviceProvider.GetRequiredService<IOptions<AbpAuditingOptions>>().Value; |
|
|
|
|
|
|
|
foreach (var selector in options.AlwaysLogSelectors) |
|
|
|
{ |
|
|
|
if (await selector(auditLogInfo)) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (options.AlwaysLogOnException && hasError) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
|
|
|
|
@ -69,7 +69,7 @@ public class AbpAuditingMiddleware : IMiddleware, ITransientDependency |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
if (ShouldWriteAuditLog(context, hasError)) |
|
|
|
if (await ShouldWriteAuditLogAsync(_auditingManager.Current.Log, context, hasError)) |
|
|
|
{ |
|
|
|
if (UnitOfWorkManager.Current != null) |
|
|
|
{ |
|
|
|
@ -98,8 +98,16 @@ public class AbpAuditingMiddleware : IMiddleware, ITransientDependency |
|
|
|
AspNetCoreAuditingOptions.IgnoredUrls.Any(x => context.Request.Path.Value.StartsWith(x)); |
|
|
|
} |
|
|
|
|
|
|
|
private bool ShouldWriteAuditLog(HttpContext httpContext, bool hasError) |
|
|
|
private async Task<bool> ShouldWriteAuditLogAsync(AuditLogInfo auditLogInfo, HttpContext httpContext, bool hasError) |
|
|
|
{ |
|
|
|
foreach (var selector in AuditingOptions.AlwaysLogSelectors) |
|
|
|
{ |
|
|
|
if (await selector(auditLogInfo)) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (AuditingOptions.AlwaysLogOnException && hasError) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
|
|
|
|
@ -2,6 +2,7 @@ |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.IO; |
|
|
|
using System.Linq.Expressions; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
namespace Volo.Abp.Auditing; |
|
|
|
|
|
|
|
@ -37,6 +38,8 @@ public class AbpAuditingOptions |
|
|
|
/// </summary>
|
|
|
|
public bool AlwaysLogOnException { get; set; } |
|
|
|
|
|
|
|
public List<Func<AuditLogInfo, Task<bool>>> AlwaysLogSelectors { get; } |
|
|
|
|
|
|
|
public List<AuditLogContributor> Contributors { get; } |
|
|
|
|
|
|
|
public List<Type> IgnoredTypes { get; } |
|
|
|
@ -55,6 +58,7 @@ public class AbpAuditingOptions |
|
|
|
IsEnabledForAnonymousUsers = true; |
|
|
|
HideErrors = true; |
|
|
|
AlwaysLogOnException = true; |
|
|
|
AlwaysLogSelectors = new List<Func<AuditLogInfo, Task<bool>>>(); |
|
|
|
|
|
|
|
Contributors = new List<AuditLogContributor>(); |
|
|
|
|
|
|
|
|
|
|
|
@ -130,7 +130,7 @@ public class AuditingInterceptor : AbpInterceptor, ITransientDependency |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
if (ShouldWriteAuditLog(invocation, options, currentUser, hasError)) |
|
|
|
if (await ShouldWriteAuditLogAsync(invocation, auditingManager.Current.Log, options, currentUser, hasError)) |
|
|
|
{ |
|
|
|
if (unitOfWorkManager.Current != null) |
|
|
|
{ |
|
|
|
@ -153,12 +153,21 @@ public class AuditingInterceptor : AbpInterceptor, ITransientDependency |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private bool ShouldWriteAuditLog( |
|
|
|
private async Task<bool> ShouldWriteAuditLogAsync( |
|
|
|
IAbpMethodInvocation invocation, |
|
|
|
AuditLogInfo auditLogInfo, |
|
|
|
AbpAuditingOptions options, |
|
|
|
ICurrentUser currentUser, |
|
|
|
bool hasError) |
|
|
|
{ |
|
|
|
foreach (var selector in options.AlwaysLogSelectors) |
|
|
|
{ |
|
|
|
if (await selector(auditLogInfo)) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (options.AlwaysLogOnException && hasError) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
|
|
|
|
@ -49,6 +49,16 @@ public class AuditTestController_Tests : AspNetCoreMvcTestBase |
|
|
|
await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_Specified_Requests() |
|
|
|
{ |
|
|
|
_options.AlwaysLogOnException = false; |
|
|
|
_options.AlwaysLogSelectors.Add(info => Task.FromResult(info.Url.Contains("api/audit-test/audit-success"))); |
|
|
|
await GetResponseAsync("api/audit-test/audit-success"); |
|
|
|
await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() |
|
|
|
{ |
|
|
|
|
|
|
|
@ -48,6 +48,15 @@ public class AuditTestPage_Tests : AspNetCoreMvcTestBase |
|
|
|
await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_Specified_Requests() |
|
|
|
{ |
|
|
|
_options.AlwaysLogOnException = false; |
|
|
|
_options.AlwaysLogSelectors.Add(info => Task.FromResult(info.Url.Contains("api/audit-test/audit-success"))); |
|
|
|
await GetResponseAsync("api/audit-test/audit-success"); |
|
|
|
await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() |
|
|
|
{ |
|
|
|
|