コード例 #1
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);//插入审计信息到数据库
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
        protected override async Task InternalInterceptAsynchronous(IInvocation invocation)
        {
            var proceedInfo = invocation.CaptureProceedInfo();

            if (AbpCrossCuttingConcerns.IsApplied(invocation.InvocationTarget, AbpCrossCuttingConcerns.Auditing))
            {
                proceedInfo.Invoke();
                var   task = (Task)invocation.ReturnValue;
                await task;
                return;
            }

            if (!_auditingHelper.ShouldSaveAudit(invocation.MethodInvocationTarget))
            {
                proceedInfo.Invoke();
                var   task = (Task)invocation.ReturnValue;
                await task;
                return;
            }

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

            var stopwatch = Stopwatch.StartNew();

            try
            {
                proceedInfo.Invoke();
                var   task = (Task)invocation.ReturnValue;
                await task;
            }
            catch (Exception ex)
            {
                auditInfo.Exception = ex;
                throw;
            }
            finally
            {
                stopwatch.Stop();
                auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);

                await _auditingHelper.SaveAsync(auditInfo).ConfigureAwait(false);
            }
        }
コード例 #4
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(
                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);
            }
        }
コード例 #5
0
        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);
                }
            }
        }
コード例 #6
0
        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);
                }
            }
        }
コード例 #7
0
        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);
                }
            }
        }
コード例 #8
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);
                }
            }
        }