Esempio n. 1
0
        /// <summary>
        /// Shoulds the intercept.
        /// </summary>
        /// <param name="invocation">The invocation.</param>
        /// <param name="auditLog">The audit log.</param>
        /// <param name="auditLogAction">The audit log action.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        protected virtual bool ShouldIntercept(
            IMethodInvocation invocation,
            out AuditLogInfo auditLog,
            out AuditLogActionInfo auditLogAction)
        {
            auditLog       = null;
            auditLogAction = null;

            if (CrossCuttingConcerns.IsApplied(invocation.TargetObject, CrossCuttingConcerns.Auditing))
            {
                return(false);
            }
            // 如果没有获取到 Scop,则返回 false。
            var auditLogScope = _auditingManager.Current;

            if (auditLogScope == null)
            {
                return(false);
            }
            // 进行二次判断是否需要存储审计日志。
            if (!_auditingHelper.ShouldSaveAudit(invocation.Method))
            {
                return(false);
            }
            // 构建审计日志信息。
            auditLog       = auditLogScope.Log;
            auditLogAction = _auditingHelper.CreateAuditLogAction(
                auditLog,
                invocation.TargetObject.GetType(),
                invocation.Method,
                invocation.Arguments
                );

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Merges the entity changes.
        /// </summary>
        /// <param name="auditLog">The audit log.</param>
        protected virtual void MergeEntityChanges(AuditLogInfo auditLog)
        {
            var changeGroups = auditLog.EntityChanges
                               .Where(e => e.ChangeType == EntityChangeType.Updated)
                               .GroupBy(e => new { e.EntityTypeFullName, e.EntityId })
                               .ToList();

            foreach (var changeGroup in changeGroups)
            {
                if (changeGroup.Count() <= 1)
                {
                    continue;
                }

                var firstEntityChange = changeGroup.First();

                foreach (var entityChangeInfo in changeGroup)
                {
                    if (entityChangeInfo == firstEntityChange)
                    {
                        continue;
                    }

                    firstEntityChange.Merge(entityChangeInfo);

                    auditLog.EntityChanges.Remove(entityChangeInfo);
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Creates the audit log action.
 /// </summary>
 /// <param name="auditLog">The audit log.</param>
 /// <param name="type">The type.</param>
 /// <param name="method">The method.</param>
 /// <param name="arguments">The arguments.</param>
 /// <returns>AuditLogActionInfo.</returns>
 public virtual AuditLogActionInfo CreateAuditLogAction(
     AuditLogInfo auditLog,
     Type type,
     MethodInfo method,
     object[] arguments)
 {
     return(CreateAuditLogAction(auditLog, type, method, CreateArgumentsDictionary(method, arguments)));
 }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DisposableSaveHandle"/> class.
 /// </summary>
 /// <param name="auditingManager">The auditing manager.</param>
 /// <param name="scope">The scope.</param>
 /// <param name="auditLog">The audit log.</param>
 /// <param name="stopWatch">The stop watch.</param>
 public DisposableSaveHandle(
     AuditingManager auditingManager,
     IDisposable scope,
     AuditLogInfo auditLog,
     Stopwatch stopWatch)
 {
     _auditingManager = auditingManager;
     _scope           = scope;
     AuditLog         = auditLog;
     StopWatch        = stopWatch;
 }
Esempio n. 5
0
        /// <summary>
        /// Creates the audit log information.
        /// </summary>
        /// <returns>AuditLogInfo.</returns>
        public virtual AuditLogInfo CreateAuditLogInfo()
        {
            var auditInfo = new AuditLogInfo
            {
                ApplicationName = Options.ApplicationName,
                UserId          = CurrentUser.Id,
                UserName        = CurrentUser.UserName,
                ClientId        = CurrentClient.Id,
                CorrelationId   = CorrelationIdProvider.Get(),
                ExecutionTime   = DateTime.Now
            };

            ExecutePreContributors(auditInfo);

            return(auditInfo);
        }
Esempio n. 6
0
        /// <summary>
        /// Executes the pre contributors.
        /// </summary>
        /// <param name="auditLogInfo">The audit log information.</param>
        protected virtual void ExecutePreContributors(AuditLogInfo auditLogInfo)
        {
            using (var scope = ServiceProvider.CreateScope())
            {
                var context = new AuditLogContributionContext(scope.ServiceProvider, auditLogInfo);

                foreach (var contributor in Options.Contributors)
                {
                    try
                    {
                        contributor.PreContribute(context);
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException(ex, LogLevel.Warning);
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Creates the audit log action.
        /// </summary>
        /// <param name="auditLog">The audit log.</param>
        /// <param name="type">The type.</param>
        /// <param name="method">The method.</param>
        /// <param name="arguments">The arguments.</param>
        /// <returns>AuditLogActionInfo.</returns>
        public virtual AuditLogActionInfo CreateAuditLogAction(
            AuditLogInfo auditLog,
            Type type,
            MethodInfo method,
            IDictionary <string, object> arguments)
        {
            var actionInfo = new AuditLogActionInfo
            {
                ServiceName = type != null
                    ? type.FullName
                    : "",
                MethodName = method.Name,
                // 序列化参数信息。
                Parameters    = SerializeConvertArguments(arguments),
                ExecutionTime = DateTime.Now
            };

            //TODO Execute contributors

            return(actionInfo);
        }
Esempio n. 8
0
 /// <summary>
 /// Saves the asynchronous.
 /// </summary>
 /// <param name="auditInfo">The audit information.</param>
 /// <returns>Task.</returns>
 public Task SaveAsync(AuditLogInfo auditInfo)
 {
     Logger.LogInformation(auditInfo.ToString());
     return(Task.FromResult(0));
 }
Esempio n. 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AuditLogContributionContext"/> class.
 /// </summary>
 /// <param name="serviceProvider">The service provider.</param>
 /// <param name="auditInfo">The audit information.</param>
 public AuditLogContributionContext(IServiceProvider serviceProvider, AuditLogInfo auditInfo)
 {
     ServiceProvider = serviceProvider;
     AuditInfo       = auditInfo;
 }