Ejemplo n.º 1
0
        public void Intercept(IInvocation invocation)
        {
            if (!_helper.ShouldAudit(invocation.MethodInvocationTarget))
            {
                invocation.Proceed();
                return;
            }

            var auditInfo = _helper.BuildAuditInfo(invocation.TargetType, invocation.MethodInvocationTarget,
                                                   invocation.Arguments);

            if (invocation.Method.IsAsync())
            {
                PerformAsyncAuditing(invocation, auditInfo);
            }
            else
            {
                PerformSyncAuditing(invocation, auditInfo);
            }
        }
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!ShouldAudit(context))
            {
                await next();

                return;
            }

            if (context.ActionDescriptor is ControllerActionDescriptor descriptor)
            {
                var auditInfo = _helper.BuildAuditInfo(
                    descriptor.ControllerTypeInfo,
                    descriptor.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);
                    _helper.Save(auditInfo);
                }
            }
        }