Skip to content

Commit

Permalink
Add an option to enable/disable AuditLogActionInfo entirely.
Browse files Browse the repository at this point in the history
Resolve #13995
  • Loading branch information
maliming committed Sep 27, 2022
1 parent 30ec6d1 commit 8a1b748
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 49 deletions.
1 change: 1 addition & 0 deletions docs/en/Audit-Logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Here, a list of the options you can configure:
* `IsEnabledForAnonymousUsers` (default: `true`): If you want to write audit logs only for the authenticated users, set this to `false`. If you save audit logs for anonymous users, you will see `null` for `UserId` values for these users.
* `AlwaysLogOnException` (default: `true`): If you set to true, it always saves the audit log on an exception/error case without checking other options (except `IsEnabled`, which completely disables the audit logging).
* `IsEnabledForGetRequests` (default: `false`): HTTP GET requests should not make any change in the database normally and audit log system doesn't save audit log objects for GET request. Set this to `true` to enable it also for the GET requests.
* `DisableLogActionInfo` (default: `false`):If you set to true, Will no longer log `AuditLogActionInfo`.
* `ApplicationName`: If multiple applications saving audit logs into a single database, set this property to your application name, so you can distinguish the logs of different applications.
* `IgnoredTypes`: A list of `Type`s to be ignored for audit logging. If this is an entity type, changes for this type of entities will not be saved. This list is also used while serializing the action parameters.
* `EntityHistorySelectors`: A list of selectors those are used to determine if an entity type is selected for saving the entity change. See the section below for details.
Expand Down
1 change: 1 addition & 0 deletions docs/zh-Hans/Audit-Logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Configure<AbpAuditingOptions>(options =>
* `AlwaysLogOnException`(默认值: `true`): 如果设置为 `true`,将始终在异常/错误情况下保存审计日志,不检查其他选项(`IsEnabled` 除外,它完全禁用了审计日志).
* `IsEnabledForGetRequests` (默认值: `false`): HTTP GET请求通常不应该在数据库进行任何更改,审计日志系统不会为GET请求保存审计日志对象. 将此值设置为 `true` 可为GET请求启用审计日志系统.
* `ApplicationName`: 如果有多个应用程序保存审计日志到单一的数据库,使用此属性设置为你的应用程序名称区分不同的应用程序日志.
* `DisableLogActionInfo` (默认值: `false`): 如果设置为 `true`, 将不再记录 `AuditLogActionInfo`.
* `IgnoredTypes`: 审计日志系统忽略的 `Type` 列表. 如果它是实体类型,则不会保存此类型实体的更改. 在序列化操作参数时也使用此列表.
* `EntityHistorySelectors`:选择器列表,用于确定是否选择了用于保存实体更改的实体类型. 有关详细信息请参阅下面的部分.
* `Contributors`: `AuditLogContributor` 实现的列表. 贡献者是扩展审计日志系统的一种方式. 有关详细信息请参阅下面的"审计日志贡献者"部分.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
finally
{
stopwatch.Stop();
auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
auditLog.Actions.Add(auditLogAction);

if (auditLogAction != null)
{
auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
auditLog.Actions.Add(auditLogAction);
}
}
}
}
Expand Down Expand Up @@ -78,12 +82,16 @@ private bool ShouldSaveAudit(ActionExecutingContext context, out AuditLogInfo au
}

auditLog = auditLogScope.Log;
auditLogAction = auditingHelper.CreateAuditLogAction(
auditLog,
context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(),
context.ActionDescriptor.AsControllerActionDescriptor().MethodInfo,
context.ActionArguments
);

if (!options.DisableLogActionInfo)
{
auditLogAction = auditingHelper.CreateAuditLogAction(
auditLog,
context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(),
context.ActionDescriptor.AsControllerActionDescriptor().MethodInfo,
context.ActionArguments
);
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext contex
finally
{
stopwatch.Stop();
auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
auditLog.Actions.Add(auditLogAction);

if (auditLogAction != null)
{
auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
auditLog.Actions.Add(auditLogAction);
}
}
}
}
Expand Down Expand Up @@ -81,12 +85,16 @@ private bool ShouldSaveAudit(PageHandlerExecutingContext context, out AuditLogIn
}

auditLog = auditLogScope.Log;
auditLogAction = auditingHelper.CreateAuditLogAction(
auditLog,
context.HandlerMethod.MethodInfo.DeclaringType,
context.HandlerMethod.MethodInfo,
context.HandlerArguments
);

if (!options.DisableLogActionInfo)
{
auditLogAction = auditingHelper.CreateAuditLogAction(
auditLog,
context.HandlerMethod.MethodInfo.DeclaringType,
context.HandlerMethod.MethodInfo,
context.HandlerArguments
);
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AbpAuditingOptions
/// Default: true.
/// </summary>
public bool AlwaysLogOnException { get; set; }

/// <summary>
/// Disables/enables audit logging for integration services.
/// Default: false.
Expand All @@ -58,6 +58,11 @@ public class AbpAuditingOptions
/// </summary>
public bool IsEnabledForGetRequests { get; set; }

/// <summary>
/// Default: false.
/// </summary>
public bool DisableLogActionInfo { get; set; }

public AbpAuditingOptions()
{
IsEnabled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override async Task InterceptAsync(IAbpMethodInvocation invocation)
var auditingManager = serviceScope.ServiceProvider.GetRequiredService<IAuditingManager>();
if (auditingManager.Current != null)
{
await ProceedByLoggingAsync(invocation, auditingHelper, auditingManager.Current);
await ProceedByLoggingAsync(invocation, auditingOptions, auditingHelper, auditingManager.Current);
}
else
{
Expand Down Expand Up @@ -74,16 +74,22 @@ protected virtual bool ShouldIntercept(IAbpMethodInvocation invocation,

private static async Task ProceedByLoggingAsync(
IAbpMethodInvocation invocation,
AbpAuditingOptions options,
IAuditingHelper auditingHelper,
IAuditLogScope auditLogScope)
{
var auditLog = auditLogScope.Log;
var auditLogAction = auditingHelper.CreateAuditLogAction(
auditLog,
invocation.TargetObject.GetType(),
invocation.Method,
invocation.Arguments
);

AuditLogActionInfo auditLogAction = null;
if (!options.DisableLogActionInfo)
{
auditLogAction = auditingHelper.CreateAuditLogAction(
auditLog,
invocation.TargetObject.GetType(),
invocation.Method,
invocation.Arguments
);
}

var stopwatch = Stopwatch.StartNew();

Expand All @@ -99,8 +105,12 @@ private static async Task ProceedByLoggingAsync(
finally
{
stopwatch.Stop();
auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
auditLog.Actions.Add(auditLogAction);

if (auditLogAction != null)
{
auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
auditLog.Actions.Add(auditLogAction);
}
}
}

Expand All @@ -117,7 +127,7 @@ private async Task ProcessWithNewAuditingScopeAsync(
{
try
{
await ProceedByLoggingAsync(invocation, auditingHelper, auditingManager.Current);
await ProceedByLoggingAsync(invocation, options, auditingHelper, auditingManager.Current);

Debug.Assert(auditingManager.Current != null);
if (auditingManager.Current.Log.Exceptions.Any())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using NSubstitute;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Auditing;
using Xunit;

Expand Down Expand Up @@ -98,4 +100,13 @@ public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Activate

await _auditingStore.Received().SaveAsync(Arg.Is<AuditLogInfo>(x => x.Exceptions.Any()));
}

[Fact]
public async Task Should_DisableLogActionInfo()
{
_options.IsEnabledForGetRequests = true;
_options.DisableLogActionInfo = true;
await GetResponseAsync("/api/audit-test/");
await _auditingStore.Received().SaveAsync(Arg.Is<AuditLogInfo>(x => x.Actions.IsNullOrEmpty()));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -97,4 +98,14 @@ public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Activate

await _auditingStore.Received().SaveAsync(Arg.Is<AuditLogInfo>(x => x.Exceptions.Any()));
}

[Fact]
public async Task Should_DisableLogActionInfo()
{
_options.IsEnabledForGetRequests = true;
_options.DisableLogActionInfo = true;

await GetResponseAsync("/Auditing/AuditTestPage");
await _auditingStore.Received().SaveAsync(Arg.Is<AuditLogInfo>(x => x.Actions.IsNullOrEmpty()));
}
}
Loading

0 comments on commit 8a1b748

Please sign in to comment.