private static IEnumerable <CustomAuditLog> GetAuditLogs(EntityEntry entityEntry, string userName, EntityState entityState)
        {
            var returnValue = new List <CustomAuditLog>();
            // var keyRepresentation = BuildKeyRepresentation(entityEntry, KeySeperator);
            DateTime RightNow = DateTime.Now;
            Guid     AuditBatchID;

            UuidCreateSequential(out AuditBatchID);

            var auditedPropertyNames =
                entityEntry.Entity.GetType()
                .GetProperties().Where(p => !p.GetCustomAttributes(typeof(DoNotAudit), true).Any())
                .Select(info => info.Name);

            var differences = new List <PropertyDiff>();


            foreach (var propertyEntry in entityEntry.Metadata.GetProperties()
                     .Where(x => auditedPropertyNames.Contains(x.Name))
                     .Select(property => new { PropertyName = property.Name, Property = entityEntry.Property(property.Name) }))
            {
                var originalValue = Convert.ToString(propertyEntry.Property.OriginalValue);
                var currentValue  = Convert.ToString(propertyEntry.Property.CurrentValue);

                if (entityState == EntityState.Modified && originalValue == currentValue) //Values are the same, don't log
                {
                    continue;
                }

                var diff = new PropertyDiff {
                    PropertyName = propertyEntry.PropertyName, OriginalValue = originalValue, NewValue = currentValue
                };
                differences.Add(diff);
            }
            var ser = JsonConvert.SerializeObject(differences);

            returnValue.Add(new CustomAuditLog
            {
                Differences   = ser,
                EventDateTime = RightNow,
                EventType     = entityState.ToString(),
                UserName      = userName,
                TableName     = entityEntry.Entity.GetType().Name
            });
            return(returnValue);
        }
Ejemplo n.º 2
0
        private static IEnumerable<CustomAuditLog> GetAuditLogs(EntityEntry entityEntry, string userName, EntityState entityState)
        {
            var returnValue = new List<CustomAuditLog>();
            // var keyRepresentation = BuildKeyRepresentation(entityEntry, KeySeperator);
            DateTime RightNow = DateTime.Now;
            Guid AuditBatchID;
            UuidCreateSequential(out AuditBatchID);

            var auditedPropertyNames =
                entityEntry.Entity.GetType()
                    .GetProperties().Where(p => !p.GetCustomAttributes(typeof(DoNotAudit), true).Any())
                    .Select(info => info.Name);

            var differences = new List<PropertyDiff>();

            foreach (var propertyEntry in entityEntry.Metadata.GetProperties()
                           .Where(x => auditedPropertyNames.Contains(x.Name))
                           .Select(property => new { PropertyName = property.Name, Property = entityEntry.Property(property.Name) }))
            {
                var originalValue = Convert.ToString(propertyEntry.Property.OriginalValue);
                var currentValue = Convert.ToString(propertyEntry.Property.CurrentValue);

                if (entityState == EntityState.Modified && originalValue == currentValue) //Values are the same, don't log
                    continue;

                var diff = new PropertyDiff { PropertyName = propertyEntry.PropertyName, OriginalValue = originalValue, NewValue = currentValue } ;
                differences.Add(diff);

            }
            var ser = JsonConvert.SerializeObject(differences);

            returnValue.Add(new CustomAuditLog
            {
                Differences = ser,
                EventDateTime = RightNow,
                EventType = entityState.ToString(),
                UserName = userName,
                TableName = entityEntry.Entity.GetType().Name
            });
            return returnValue;
        }