|
|
|
|
@ -0,0 +1,72 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Volo.Abp.Aspects;
|
|
|
|
|
using Volo.Abp.Auditing;
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace Volo.Abp.AspNetCore.Mvc.Auditing
|
|
|
|
|
{
|
|
|
|
|
public class AbpAuditActionFilter : IAsyncActionFilter, ITransientDependency
|
|
|
|
|
{
|
|
|
|
|
protected AuditingOptions Options { get; }
|
|
|
|
|
private readonly IAuditingHelper _auditingHelper;
|
|
|
|
|
|
|
|
|
|
public AbpAuditActionFilter(IOptions<AuditingOptions> options, IAuditingHelper auditingHelper)
|
|
|
|
|
{
|
|
|
|
|
Options = options.Value;
|
|
|
|
|
_auditingHelper = auditingHelper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
|
|
|
|
{
|
|
|
|
|
if (!ShouldSaveAudit(context))
|
|
|
|
|
{
|
|
|
|
|
await next();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (AbpCrossCuttingConcerns.Applying(context.Controller, AbpCrossCuttingConcerns.Auditing))
|
|
|
|
|
{
|
|
|
|
|
var auditInfo = _auditingHelper.CreateAuditInfo(
|
|
|
|
|
context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(),
|
|
|
|
|
context.ActionDescriptor.AsControllerActionDescriptor().MethodInfo,
|
|
|
|
|
context.ActionArguments
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var stopwatch = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = await next();
|
|
|
|
|
if (result.Exception != null && !result.ExceptionHandled)
|
|
|
|
|
{
|
|
|
|
|
auditInfo.Exception = result.Exception;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
auditInfo.Exception = ex;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
|
|
|
|
|
auditInfo.ServiceName = "AbpAuditActionFilter->" + auditInfo.ServiceName;
|
|
|
|
|
await _auditingHelper.SaveAsync(auditInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ShouldSaveAudit(ActionExecutingContext actionContext)
|
|
|
|
|
{
|
|
|
|
|
return Options.IsEnabled &&
|
|
|
|
|
actionContext.ActionDescriptor.IsControllerAction() &&
|
|
|
|
|
_auditingHelper.ShouldSaveAudit(actionContext.ActionDescriptor.GetMethodInfo(), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|