示例#1
0
        private void SaveAuditLog()
        {
            string username = string.Empty;

            var dbEntityEntries = ChangeTracker.Entries().ToList()
                                  .Where(p => p.State == EntityState.Modified || p.State == EntityState.Added || p.State == EntityState.Deleted);

            foreach (var dbEntityEntry in dbEntityEntries)
            {
                try
                {
                    username = ((BaseEntity)dbEntityEntry.Entity).ModifiedBy;
                    var auditLogs = AuditLogHelper.GetChangesForAuditLog(dbEntityEntry, username);
                    foreach (var auditlog in auditLogs)
                    {
                        if (auditlog != null)
                        {
                            AuditLogs.Add(auditlog);
                        }
                    }
                }
                catch
                {
                    continue;
                }
            }
        }
        public int SaveChanges(string userName, string ipAddress)
        {
            var changedEntities = ChangeTracker.Entries().Where(p => p.State == EntityState.Deleted || p.State == EntityState.Modified);
            var addedEntities   = ChangeTracker.Entries().Where(p => p.State == EntityState.Added).ToList();

            foreach (var entry in changedEntities)
            {
                var auditLog = GetAuditLog(entry, userName, ipAddress);
                AuditLogs.Add(auditLog);
            }

            var objectsCount = base.SaveChanges(); //need to do a save here so the added entities can get an Identity Id

            foreach (var entry in addedEntities)
            {
                entry.State = EntityState.Added;     //need to set back to added, because we did a savechanges, which makes it "Unchanged" now
                var auditLog = GetAuditLog(entry, userName, ipAddress);
                entry.State = EntityState.Unchanged; //back to "Unchanged" so it doesnt get double created
                AuditLogs.Add(auditLog);
            }

            objectsCount += base.SaveChanges();

            return(objectsCount);
        }
示例#3
0
        private void SaveAuditTrail(string currentUserDisplayName)
        {
            ChangeTracker.DetectChanges();
            var auditEntries = new List <AuditLog>();

            foreach (EntityEntry entry in ChangeTracker.Entries())
            {
                if (entry.Entity is AuditLog || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
                {
                    continue;
                }

                var auditEntry = new AuditLog()
                {
                    TableName = entry.Entity.GetType().Name,
                    CreatedBy = currentUserDisplayName,
                    CreatedAt = _dateTimeProvider.GetUtcNow()
                };

                auditEntries.Add(auditEntry);

                foreach (PropertyEntry property in entry.Properties)
                {
                    string propertyName = property.Metadata.Name;
                    if (property.Metadata.IsPrimaryKey())
                    {
                        Dictionary <string, Guid> foo = JsonSerializer.Deserialize <Dictionary <string, Guid> >(auditEntry.KeyValues, null);
                        foo[propertyName]    = (Guid)property.CurrentValue;
                        auditEntry.KeyValues = JsonSerializer.Serialize(foo);
                        continue;
                    }
                    switch (entry.State)
                    {
                    case EntityState.Added:
                        auditEntry.Type = AuditLogType.Create;
                        auditEntry.NewValues[propertyName] = property.CurrentValue;
                        break;

                    case EntityState.Deleted:
                        auditEntry.Type = AuditLogType.Delete;
                        auditEntry.OldValues[propertyName] = property.OriginalValue;
                        break;

                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            auditEntry.ChangedColumns.Add(propertyName);
                            auditEntry.Type = AuditLogType.Update;
                            auditEntry.OldValues[propertyName] = property.OriginalValue;
                            auditEntry.NewValues[propertyName] = property.CurrentValue;
                        }
                        break;
                    }
                }
            }
            foreach (AuditLog auditEntry in auditEntries)
            {
                AuditLogs.Add(auditEntry);
            }
        }
        private void OnBeforeSaveChanges(long?userId = null, string userName = null, string machineName = null)
        {
            ChangeTracker.DetectChanges();
            var auditEntries = new List <AuditEntry>();

            foreach (var entry in ChangeTracker.Entries())
            {
                if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
                {
                    continue;
                }
                var auditEntry = new AuditEntry(entry)
                {
                    TableName   = entry.Entity.GetType().Name,
                    UserId      = userId,
                    UserName    = userName,
                    MachineName = machineName
                };
                auditEntries.Add(auditEntry);
                foreach (var property in entry.Properties)
                {
                    string propertyName = property.Metadata.Name;
                    if (property.Metadata.IsPrimaryKey())
                    {
                        auditEntry.KeyValues[propertyName] = property.CurrentValue;
                        continue;
                    }
                    switch (entry.State)
                    {
                    case EntityState.Added:
                        auditEntry.AuditType = AuditType.Create;
                        auditEntry.NewValues[propertyName] = property.CurrentValue;
                        break;

                    case EntityState.Deleted:
                        auditEntry.AuditType = AuditType.Delete;
                        auditEntry.OldValues[propertyName] = property.OriginalValue;
                        break;

                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            auditEntry.ChangedColumns.Add(propertyName);
                            auditEntry.AuditType = AuditType.Update;
                            auditEntry.OldValues[propertyName] = property.OriginalValue;
                            auditEntry.NewValues[propertyName] = property.CurrentValue;
                        }
                        break;
                    }
                }
            }

            foreach (var auditEntry in auditEntries)
            {
                AuditLogs.Add(auditEntry.ToAudit());
            }
        }
示例#5
0
        public override int SaveChanges()
        {
            var entries = ChangeTracker.Entries().GetEnumerator();

            entries.MoveNext();

            var entry = entries.Current;

            if (entry == null)
            {
                return(0);
            }

            AuditLog log = new AuditLog();

            log.Operation = entry.State.ToString().Substring(0, 1);
            log.Entity    = entry.Entity.ToString().Replace("PRS.Models.", "");

            if (entry.State != EntityState.Added)
            {
                log.EntityKey = GetPrimaryKeyValue(entry.Entity);
            }

            log.UserName = UserName;

            log.OperationDate = DateTime.Now;

            AuditLogs.Add(log);

            int count = base.SaveChanges();

            if (log.EntityKey == 0)
            {
                log.EntityKey    = GetPrimaryKeyValue(entry.Entity);
                Entry(log).State = EntityState.Modified;
                base.SaveChanges();
            }

            return(count);
        }
示例#6
0
        private Task OnAfterSaveChanges(List <AuditEntry> auditEntries)
        {
            if (auditEntries == null || auditEntries.Count == 0)
            {
                return(Task.CompletedTask);
            }

            foreach (var auditEntry in auditEntries)
            {
                foreach (var prop in auditEntry.TemporaryProperties)
                {
                    if (prop.Metadata.IsPrimaryKey())
                    {
                        auditEntry.KeyValues[prop.Metadata.Name] = prop.CurrentValue;
                    }
                    else
                    {
                        auditEntry.NewValues[prop.Metadata.Name] = prop.CurrentValue;
                    }
                }
                AuditLogs.Add(auditEntry.ToAudit());
            }
            return(SaveChangesAsync());
        }
示例#7
0
        private List <AuditEntry> OnBeforeSaveChanges(string userId)
        {
            ChangeTracker.DetectChanges();
            var auditEntries = new List <AuditEntry>();

            foreach (var entry in ChangeTracker.Entries())
            {
                if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
                {
                    continue;
                }

                var auditEntry = new AuditEntry(entry);
                auditEntry.TableName = entry.Entity.GetType().Name;
                auditEntry.UserId    = userId;
                auditEntries.Add(auditEntry);
                foreach (var property in entry.Properties)
                {
                    if (property.IsTemporary)
                    {
                        auditEntry.TemporaryProperties.Add(property);
                        continue;
                    }

                    string propertyName = property.Metadata.Name;
                    if (property.Metadata.IsPrimaryKey())
                    {
                        auditEntry.KeyValues[propertyName] = property.CurrentValue;
                        continue;
                    }

                    switch (entry.State)
                    {
                    case EntityState.Added:
                        auditEntry.AuditType = AuditType.Create;
                        auditEntry.NewValues[propertyName] = property.CurrentValue;
                        break;

                    case EntityState.Deleted:
                        auditEntry.AuditType = AuditType.Delete;
                        auditEntry.OldValues[propertyName] = property.OriginalValue;
                        break;

                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            auditEntry.ChangedColumns.Add(propertyName);
                            auditEntry.AuditType = AuditType.Update;
                            auditEntry.OldValues[propertyName] = property.OriginalValue;
                            auditEntry.NewValues[propertyName] = property.CurrentValue;
                        }
                        break;
                    }
                }
            }
            foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties))
            {
                AuditLogs.Add(auditEntry.ToAudit());
            }
            return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList());
        }
示例#8
0
        private void TrackChanges()
        {
            var modifiedEntities = ChangeTracker.Entries()
                                   .Where(p => p.State == EntityState.Modified).ToList();

            var now = DateTime.UtcNow;

            foreach (var change in modifiedEntities)
            {
                if (change.Entity is AuditedEntity)
                {
                    var primaryKey = GetPrimaryKeyValue(change);
                    var entityName = GetTableName(change);

                    foreach (var prop in change.OriginalValues.PropertyNames)
                    {
                        if (prop == "ModifyUser" || prop == "ModifyDate")
                        {
                            continue;
                        }
                        string originalValue = string.Empty;
                        string currentValue  = string.Empty;
                        if (change.OriginalValues[prop] != null)
                        {
                            originalValue = change.OriginalValues[prop].ToString();
                        }

                        if (change.CurrentValues[prop] != null)
                        {
                            currentValue = change.CurrentValues[prop].ToString();
                        }
                        if (originalValue != currentValue)
                        {
                            AuditLog log = new AuditLog()
                            {
                                EntityName      = entityName,
                                PrimaryKeyValue = (int)primaryKey,
                                PropertyName    = prop,
                                OldValue        = originalValue,
                                NewValue        = currentValue,
                                DateChanged     = now,
                                UserName        = KontoGlobals.UserName,
                                MenuId          = KontoGlobals.MenuId
                            };

                            log.EntryMode = Convert.ToBoolean(change.Property("IsDeleted").CurrentValue)
                                ? "Delete"
                                : "Edit";

                            AuditLogs.Add(log);
                        }
                    }
                }
            }

            foreach (var entry in this.ChangeTracker.Entries().Where(e => e.State == EntityState.Added || e.State == EntityState.Modified))
            {
                if (entry.Entity is AuditedEntity)
                {
                    var auditable = entry.Entity as AuditedEntity;
                    if (entry.State == EntityState.Added)
                    {
                        auditable.RowId = Guid.NewGuid();
                        //auditable.CreatedBy = UserProvider;//
                        auditable.CreateDate = TimestampProvider();
                        auditable.CreateUser = KontoGlobals.UserName;
                        //auditable.UpdatedOn = TimestampProvider();
                    }
                    else
                    {
                        //auditable.UpdatedBy = UserProvider;
                        auditable.ModifyUser = KontoGlobals.UserName;
                        auditable.ModifyDate = TimestampProvider();
                    }
                    auditable.IpAddress = UserIpAddress;
                }
            }
        }