コード例 #1
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            IAuditingConfiguration configuration = _provider.GetRequiredService <IAuditingConfiguration>();

            if (!ShouldSaveAudit(context, configuration))
            {
                await next();

                return;
            }

            using (CrossCuttingConcerns.Applying(context.Controller, CrossCuttingConcerns.Auditing))
            {
                IAuditStore store    = _provider.GetService <IAuditStore>();
                IFunction   function = context.GetExecuteFunction();
                //var auditInfo = store.CreateAuditInfo(
                //    context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(),
                //    context.ActionDescriptor.AsControllerActionDescriptor().MethodInfo,
                //    context.ActionArguments
                //);
                Type                type         = context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType();
                List <Type>         ignoredTypes = configuration.IgnoredTypes;
                AuditOperationEntry operation    = new AuditOperationEntry
                {
                    FunctionName    = function.Name,
                    ClientIpAddress = context.HttpContext.GetClientIp(),
                    UserAgent       = context.HttpContext.Request.Headers["User-Agent"].FirstOrDefault(),
                    CreatedTime     = DateTime.Now,
                    ServiceName     = type != null
                    ? type.FullName
                    : "",
                    Parameters = ConvertArgumentsToJson(context.ActionArguments, ignoredTypes),
                };
                if (context.HttpContext.User.Identity.IsAuthenticated && context.HttpContext.User.Identity is ClaimsIdentity identity)
                {
                    operation.UserId   = identity.GetUserId();
                    operation.UserName = identity.GetUserName();
                }

                var stopwatch = Stopwatch.StartNew();

                ActionExecutedContext result = null;
                try
                {
                    result = await next();

                    if (result.Exception != null && !result.ExceptionHandled)
                    {
                        operation.Exception = result.Exception;
                    }
                }
                catch (Exception ex)
                {
                    operation.Exception = ex;
                    throw;
                }
                finally
                {
                    stopwatch.Stop();
                    operation.Elapsed = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);

                    if (configuration.SaveReturnValues && result != null)
                    {
                        switch (result.Result)
                        {
                        case ObjectResult objectResult:
                            operation.ReturnValue = AuditingHelper.Serialize(objectResult.Value, ignoredTypes);
                            break;

                        case JsonResult jsonResult:
                            operation.ReturnValue = AuditingHelper.Serialize(jsonResult.Value, ignoredTypes);
                            break;

                        case ContentResult contentResult:
                            operation.ReturnValue = contentResult.Content;
                            break;

                        case AjaxResult ajaxResult:
                            operation.ReturnValue = ajaxResult.Content;
                            break;
                        }
                    }

                    await store.SaveAsync(operation);
                }
            }
        }