示例#1
0
        public virtual async Task RecordSave(IEntity entity, SaveMode saveMode)
        {
            if (!LogEventsAttribute.ShouldLog(entity.GetType()))
            {
                return;
            }

            var eventInfo = CreateApplicationEvent();

            if (eventInfo == null)
            {
                return;
            }

            eventInfo.ItemType = entity.GetType().FullName;
            eventInfo.ItemKey  = entity.GetId().ToString();
            eventInfo.Event    = saveMode.ToString();
            eventInfo.IP       = GetCurrentUserIP();
            eventInfo.UserId   = GetCurrentUserId();

            if (saveMode == SaveMode.Update)
            {
                var changes = await GetChangesXml(entity);

                if (changes.IsEmpty()) // No changes have happened, ignore recording the action:
                {
                    return;
                }

                eventInfo.Data = changes;
            }
            else
            {
                if (!ShouldSkipInsertData)
                {
                    eventInfo.Data = GetDataXml(entity);
                }
            }

            if (OnRecordingSave.IsHandled())
            {
                var args = new AuditSaveEventArgs
                {
                    SaveMode         = saveMode,
                    ApplicationEvent = eventInfo,
                    Entity           = entity
                };

                await OnRecordingSave.Raise(args);

                if (args.Cancel)
                {
                    return;
                }
            }

            await Save(eventInfo);

            ProcessContext <UndoContext> .Current.Perform(c => c.Append(eventInfo, entity));
        }
示例#2
0
        public virtual async Task RecordDelete(IEntity entity)
        {
            if (!LogEventsAttribute.ShouldLog(entity.GetType()))
            {
                return;
            }

            var eventInfo = CreateApplicationEvent();

            if (eventInfo == null)
            {
                return;
            }

            eventInfo.ItemType = entity.GetType().FullName;
            eventInfo.ItemKey  = entity.GetId().ToString();
            eventInfo.Event    = "Delete";
            eventInfo.IP       = GetCurrentUserIP();
            eventInfo.UserId   = GetCurrentUserId();

            var changes = Entity.Database.GetProvider(entity.GetType()).GetUpdatedValues(entity, null);

            eventInfo.Data = ToChangeXml(changes);

            if (OnRecordingDelete.IsHandled())
            {
                var args = new AuditDeleteEventArgs {
                    ApplicationEvent = eventInfo, Entity = entity
                };
                await OnRecordingDelete.Raise(args);

                if (args.Cancel)
                {
                    return;
                }
            }

            await Save(eventInfo);

            ProcessContext <UndoContext> .Current.Perform(c => c.Append(eventInfo, entity));
        }