Example #1
0
        public async Task <HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func <Task <HttpResponseMessage> > continuation)
        {
            var method = actionContext.ActionDescriptor.GetMethodInfoOrNull();

            if (method == null || !ShouldSaveAudit(actionContext))
            {
                return(await continuation());
            }

            var auditInfo = _auditingHelper.CreateAuditInfo(
                method,
                actionContext.ActionArguments
                );

            var stopwatch = Stopwatch.StartNew();

            try
            {
                return(await continuation());
            }
            catch (Exception ex)
            {
                auditInfo.Exception = ex;
                throw;
            }
            finally
            {
                stopwatch.Stop();
                auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
                await _auditingHelper.SaveAsync(auditInfo);
            }
        }
        public void Intercept(IInvocation invocation)
        {
            if (AbpCrossCuttingConcerns.IsApplied(invocation.InvocationTarget, AbpCrossCuttingConcerns.Auditing))
            {
                invocation.Proceed();
                return;
            }

            if (!_auditingHelper.ShouldSaveAudit(invocation.MethodInvocationTarget))
            {
                invocation.Proceed();
                return;
            }

            var auditInfo = _auditingHelper.CreateAuditInfo(invocation.TargetType, invocation.MethodInvocationTarget, invocation.Arguments);

            if (AsyncHelper.IsAsyncMethod(invocation.Method))
            {
                PerformAsyncAuditing(invocation, auditInfo);
            }
            else
            {
                PerformSyncAuditing(invocation, auditInfo);
            }
        }
Example #3
0
        public void CreateAuditInfo()
        {
            var actual = _auditingHelper.CreateAuditInfo();

            actual.ShouldBeOfType <AuditInfo>().ShouldNotBeNull();
            actual.CurrentUser.ShouldBe("TestUser");
        }
Example #4
0
        public void Intercept(IInvocation invocation)
        {
            //判断过滤器是否已经处理了过了
            if (AbpCrossCuttingConcerns.IsApplied(invocation.InvocationTarget, AbpCrossCuttingConcerns.Auditing))
            {
                invocation.Proceed();
                return;
            }
            //通过 IAuditingHelper 来判断当前方法是否需要记录审计日志信息
            if (!_auditingHelper.ShouldSaveAudit(invocation.MethodInvocationTarget))
            {
                invocation.Proceed();
                return;
            }
            // 构造审计信息
            var auditInfo = _auditingHelper.CreateAuditInfo(invocation.TargetType, invocation.MethodInvocationTarget, invocation.Arguments);

            //判断方法的类型,同步方法与异步方法的处理逻辑不一样
            if (invocation.Method.IsAsync())
            {
                PerformAsyncAuditing(invocation, auditInfo);
            }
            else
            {
                PerformSyncAuditing(invocation, auditInfo);
            }
        }
Example #5
0
        public async Task <HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func <Task <HttpResponseMessage> > continuation)
        {
            var actionDescriptor = actionContext.ActionDescriptor as ReflectedHttpActionDescriptor;

            if (actionDescriptor == null || !_auditingHelper.ShouldSaveAudit(actionDescriptor?.MethodInfo, true))//此时说明不需要审计
            {
                return(await continuation());
            }

            var auditInfo = _auditingHelper.CreateAuditInfo(
                actionContext.ActionDescriptor.ControllerDescriptor.ControllerType,
                actionDescriptor.MethodInfo,
                actionContext.ActionArguments);

            var stopwatch = Stopwatch.StartNew();

            try
            {
                return(await continuation());
            }
            catch (Exception ex)
            {
                auditInfo.Exception = ex;//若发生异常则保存异常
                throw;
            }
            finally
            {
                stopwatch.Stop();
                auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
                await _auditingHelper.SaveAsync(auditInfo);//插入审计信息到数据库
            }
        }
Example #6
0
        public IAuditSaveHandle BeginScope()
        {
            var ambientScope = _ambientScopeProvider.BeginScope(
                _ambientContextKey,
                new AuditScope(_auditingHelper.CreateAuditInfo())
                );

            Debug.Assert(Current != null, "Current != null");

            return(new DisposableSaveHandle(this, ambientScope, Current.Info, Stopwatch.StartNew()));
        }
        public override void InterceptSynchronous(IInvocation invocation)
        {
            if (AbpCrossCuttingConcerns.IsApplied(invocation.InvocationTarget, AbpCrossCuttingConcerns.Auditing))
            {
                invocation.Proceed();
                return;
            }

            if (!_auditingHelper.ShouldSaveAudit(invocation.MethodInvocationTarget))
            {
                invocation.Proceed();
                return;
            }

            var auditInfo = _auditingHelper.CreateAuditInfo(invocation.TargetType, invocation.MethodInvocationTarget, invocation.Arguments);

            var stopwatch = Stopwatch.StartNew();

            try
            {
                invocation.Proceed();
            }
            catch (Exception ex)
            {
                auditInfo.Exception = ex;
                throw;
            }
            finally
            {
                stopwatch.Stop();
                auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);

                if (_auditingConfiguration.SaveReturnValues && invocation.ReturnValue != null)
                {
                    auditInfo.ReturnValue = _auditSerializer.Serialize(invocation.ReturnValue);
                }

                _auditingHelper.Save(auditInfo);
            }
        }
Example #8
0
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!ShouldSaveAudit(filterContext))
            {
                AprilAuditFilterData.Set(filterContext.HttpContext, null);//如果不需要审计则保存一个null
                return;
            }

            var auditInfo = _auditingHelper.CreateAuditInfo(filterContext.ActionDescriptor.ControllerDescriptor.ControllerType,
                                                            filterContext.ActionDescriptor.GetMethodInfoOrNull(),
                                                            filterContext.ActionParameters);

            var stopwatch = Stopwatch.StartNew();

            AprilAuditFilterData.Set(filterContext.HttpContext, new AprilAuditFilterData(stopwatch, auditInfo));//将AprilAuditFilterData保存到httpContext.item中
        }
        public async Task <HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func <Task <HttpResponseMessage> > continuation)
        {
            var method = actionContext.ActionDescriptor.GetMethodInfoOrNull();

            if (method == null || !ShouldSaveAudit(actionContext))
            {
                return(await continuation());
            }

            var auditInfo = _auditingHelper.CreateAuditInfo(
                actionContext.ActionDescriptor.ControllerDescriptor.ControllerType,
                method,
                actionContext.ActionArguments
                );

            var stopwatch = Stopwatch.StartNew();

            HttpResponseMessage response = null;

            try
            {
                response = await continuation();

                return(response);
            }
            catch (Exception ex)
            {
                auditInfo.Exception = ex;
                throw;
            }
            finally
            {
                stopwatch.Stop();
                auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);

                if (_auditingConfiguration.SaveReturnValues && response != null)
                {
                    if (response.TryGetContentValue(out object resultObject))
                    {
                        auditInfo.ReturnValue = _auditSerializer.Serialize(resultObject);
                    }
                }

                await _auditingHelper.SaveAsync(auditInfo);
            }
        }
Example #10
0
        public void Intercept(IInvocation invocation)
        {
            if (!_auditingHelper.ShouldSaveAudit(invocation.MethodInvocationTarget))
            {
                invocation.Proceed();
                return;
            }
            var auditInfo = _auditingHelper.CreateAuditInfo(invocation.TargetType, invocation.MethodInvocationTarget, invocation.Arguments);

            if (invocation.Method.ReturnType == typeof(Task) ||
                (invocation.Method.ReturnType.GetTypeInfo().IsGenericType&& invocation.Method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>)))
            {
                PerformAsyncAuditing(invocation, auditInfo);
            }
            else
            {
                PerformSyncAuditing(invocation, auditInfo);
            }
        }
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!ShouldSaveAudit(context))
            {
                await next();

                return;
            }

            using (CodeZeroCrossCuttingConcerns.Applying(context.Controller, CodeZeroCrossCuttingConcerns.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);
                    await _auditingHelper.SaveAsync(auditInfo);
                }
            }
        }
        public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
        {
            if (context.HandlerMethod == null || !ShouldSaveAudit(context))
            {
                await next();

                return;
            }

            using (AbpCrossCuttingConcerns.Applying(context.HandlerInstance, AbpCrossCuttingConcerns.Auditing))
            {
                var auditInfo = _auditingHelper.CreateAuditInfo(
                    context.HandlerInstance.GetType(),
                    context.HandlerMethod.MethodInfo,
                    context.GetBoundPropertiesAsDictionary()
                    );

                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);
                    await _auditingHelper.SaveAsync(auditInfo);
                }
            }
        }
Example #13
0
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!ShouldSaveAudit(filterContext))
            {
                AbpAuditFilterData.Set(filterContext.HttpContext, null);
                return;
            }

            var auditInfo = _auditingHelper.CreateAuditInfo(
                filterContext.ActionDescriptor.GetMethodInfoOrNull(),
                filterContext.ActionParameters
                );

            var actionStopwatch = Stopwatch.StartNew();

            AbpAuditFilterData.Set(
                filterContext.HttpContext,
                new AbpAuditFilterData(
                    actionStopwatch,
                    auditInfo
                    )
                );
        }
        public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
        {
            if (context.HandlerMethod == null || !ShouldSaveAudit(context))
            {
                await next();

                return;
            }

            using (AbpCrossCuttingConcerns.Applying(context.HandlerInstance, AbpCrossCuttingConcerns.Auditing))
            {
                var auditInfo = _auditingHelper.CreateAuditInfo(
                    context.HandlerInstance.GetType(),
                    context.HandlerMethod.MethodInfo,
                    context.GetBoundPropertiesAsDictionary()
                    );

                var stopwatch = Stopwatch.StartNew();

                PageHandlerExecutedContext result = null;
                try
                {
                    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);

                    if (_auditingConfiguration.SaveReturnValues && result != null)
                    {
                        switch (result.Result)
                        {
                        case ObjectResult objectResult:
                            if (objectResult.Value is AjaxResponse ajaxObjectResponse)
                            {
                                auditInfo.ReturnValue = _auditSerializer.Serialize(ajaxObjectResponse.Result);
                            }
                            else
                            {
                                auditInfo.ReturnValue = _auditSerializer.Serialize(objectResult.Value);
                            }
                            break;

                        case JsonResult jsonResult:
                            if (jsonResult.Value is AjaxResponse ajaxJsonResponse)
                            {
                                auditInfo.ReturnValue = _auditSerializer.Serialize(ajaxJsonResponse.Result);
                            }
                            else
                            {
                                auditInfo.ReturnValue = _auditSerializer.Serialize(jsonResult.Value);
                            }
                            break;

                        case ContentResult contentResult:
                            auditInfo.ReturnValue = contentResult.Content;
                            break;
                        }
                    }

                    await _auditingHelper.SaveAsync(auditInfo);
                }
            }
        }
Example #15
0
        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();

                ActionExecutedContext result = null;
                try
                {
                    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);

                    if (_auditingConfiguration.SaveReturnValues && result != null)
                    {
                        switch (result.Result)
                        {
                        case ObjectResult objectResult:
                            auditInfo.ReturnValue = _auditSerializer.Serialize(objectResult.Value);
                            break;

                        case JsonResult jsonResult:
                            auditInfo.ReturnValue = _auditSerializer.Serialize(jsonResult.Value);
                            break;

                        case ContentResult contentResult:
                            auditInfo.ReturnValue = contentResult.Content;
                            break;
                        }
                    }

                    await _auditingHelper.SaveAsync(auditInfo);
                }
            }
        }