Exemplo n.º 1
0
        private Task OnAfterSaveChanges(List <AuditEntry> auditEntries)
        {
            if (auditEntries == null || auditEntries.Count == 0)
            {
                return(Task.CompletedTask);
            }

            foreach (var auditEntry in auditEntries)
            {
                // Get the final value of the temporary properties
                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;
                    }
                }

                // Save the Audit entry
                TblAudit.Add(auditEntry.ToAudit());
            }

            return(SaveChangesAsync());
        }
Exemplo n.º 2
0
        public TblAudit ToAudit()
        {
            var audit = new TblAudit
            {
                TableName = TableName,
                DateTime  = DateTime.UtcNow,
                Action    = Action,
                UserId    = UserId,
                KeyValues = KeyValues.First().Value.ToString(),
                OldValues = OldValues.Count == 0 ? null : JsonConvert.SerializeObject(OldValues),
                NewValues = NewValues.Count == 0 ? null : JsonConvert.SerializeObject(NewValues),
            };


            List <AuditDelta> DeltaList = new List <AuditDelta>();

            JObject sourceJObject = JsonConvert.DeserializeObject <JObject>(audit.OldValues);

            if (audit.Action == "Modified")
            {
                JObject targetJObject = JsonConvert.DeserializeObject <JObject>(audit.NewValues);

                foreach (KeyValuePair <string, JToken> sourceProperty in sourceJObject)
                {
                    JProperty targetProp = targetJObject.Property(sourceProperty.Key);

                    if (!JToken.DeepEquals(sourceProperty.Value, targetProp.Value))
                    {
                        AuditDelta delta = new AuditDelta
                        {
                            FieldName   = sourceProperty.Key,
                            ValueBefore = Convert.ToString(sourceProperty.Value),
                            ValueAfter  = Convert.ToString(targetProp.Value)
                        };
                        DeltaList.Add(delta);
                    }
                }
            }
            else
            {
                foreach (KeyValuePair <string, JToken> sourceProperty in sourceJObject)
                {
                    AuditDelta delta = new AuditDelta
                    {
                        FieldName   = sourceProperty.Key,
                        ValueBefore = Convert.ToString(sourceProperty.Value),
                        ValueAfter  = string.Empty
                    };
                    DeltaList.Add(delta);
                }
            }



            audit.Changes = JsonConvert.SerializeObject(DeltaList);

            return(audit);
        }
Exemplo n.º 3
0
        private List <AuditEntry> OnBeforeSaveChanges()
        {
            ChangeTracker.DetectChanges();
            var auditEntries = new List <AuditEntry>();

            foreach (var entry in ChangeTracker.Entries())
            {
                if (IsAuditDisabled(entry))
                {
                    continue;
                }

                var auditEntry = new AuditEntry(entry)
                {
                    TableName = entry.Metadata.Relational().TableName
                };
                auditEntries.Add(auditEntry);

                foreach (var property in entry.Properties)
                {
                    if (property.IsTemporary)
                    {
                        // value will be generated by the database, get the value after saving
                        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.NewValues[propertyName] = property.CurrentValue;
                    //    break;

                    case EntityState.Deleted:
                        auditEntry.OldValues[propertyName] = property.OriginalValue;
                        auditEntry.Action = "Deleted";
                        auditEntry.UserId = CommonHelper.UserId;
                        break;

                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            auditEntry.Action = "Modified";
                            auditEntry.OldValues[propertyName] = entry.GetDatabaseValues().GetValue <object>(propertyName);
                            auditEntry.NewValues[propertyName] = property.CurrentValue;
                            auditEntry.UserId = CommonHelper.UserId;
                        }
                        break;
                    }
                }
            }

            // Save audit entities that have all the modifications
            foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties))
            {
                TblAudit.Add(auditEntry.ToAudit());
            }

            // keep a list of entries where the value of some properties are unknown at this step
            return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList());
        }