コード例 #1
0
        public void ApplyAuditLog(DbContext workingContext, DbEntityEntry entry, LogOperation logOperation)
        {
            var includedProperties = new List <string>();
            var entityKey          = workingContext.GetEntityKey(entry.Entity);
            var entityType         = entry.Entity.GetType();

            if (entry.IsAttr <AuditableAttribute>())
            {
                var props = entityType.GetProperties().Where(pi => !pi.IsAttr <NotAuditableAttrubute>());
                includedProperties.AddRange(props.Select(pi => pi.Name));
            }
            else
            {
                var props = entityType.GetProperties()
                            .Where(p => p.IsAttr <AuditableAttribute>() && !p.IsAttr <NotAuditableAttrubute>());

                includedProperties.AddRange(props.Select(pi => pi.Name));
            }

            if (entry.State == EntityState.Modified)
            {
                var originalValues    = workingContext.Entry(entry.Entity).GetDatabaseValues();
                var changedProperties = (from propertyName in originalValues.PropertyNames
                                         let propertyEntry = entry.Property(propertyName)
                                                             let currentValue = propertyEntry.CurrentValue
                                                                                let originalValue = originalValues[propertyName]
                                                                                                    where (!Equals(currentValue, originalValue) && includedProperties.Contains(propertyName))
                                                                                                    select new ChangedProperty
                {
                    Name = propertyName,
                    CurrentValue = currentValue,
                    OriginalValue = originalValue
                }).ToArray();

                if (changedProperties.Any())
                {
                    foreach (var log in changedProperties.Select(changedProperty => new AuditLog
                    {
                        Created = DateTime.Now,
                        EntityFullName = entry.Entity.GetType().FullName,
                        Entity = Utils.Serialize(entry.Entity),
                        EntityIdBytes = Utils.Serialize(entry.Entity),
                        Operation = logOperation,
                        OldValue = changedProperty.OriginalValue.ToString(),
                        NewValue = changedProperty.CurrentValue.ToString(),
                        PropertyName = changedProperty.Name
                    }))
                    {
                        AuditLogs.Add(log);
                    }

                    SaveChanges();
                }
            }
            else
            {
                var log = new AuditLog
                {
                    Created        = DateTime.Now,
                    EntityFullName = entry.Entity.GetType().FullName,
                    Entity         = Utils.Serialize(entry.Entity),
                    EntityIdBytes  = Utils.Serialize(entityKey),
                    Operation      = logOperation,
                };

                AuditLogs.Add(log);
                SaveChanges();
            }
        }
コード例 #2
0
ファイル: DbContextExts.cs プロジェクト: nbulusanjr/EF.Audit
        /// <summary>
        /// Register audit information
        /// </summary>
        /// <param name="context">The current context</param>
        /// <param name="entry">DbContext entry to audit</param>
        /// <param name="logOperation">Audit operation</param>
        private static void ApplyAuditLog(DbContext context, DbEntityEntry entry, LogOperation logOperation)
        {
            var currentPrincipal   = Thread.CurrentPrincipal;
            var user               = currentPrincipal != null? (currentPrincipal.Identity).Name:string.Empty;
            var includedProperties = new List <string>();
            var entityKey          = context.GetEntityKey(entry.Entity).GetEntityString();
            var entityType         = entry.Entity.GetType();

            if (entry.IsAttr <AuditableAttribute>())
            {
                var props = entityType.GetProperties().Where(pi => !pi.IsAttr <NotAuditableAttribute>());
                includedProperties.AddRange(props.Select(pi => pi.Name));
            }
            else
            {
                var props = entityType.GetProperties()
                            .Where(p => p.IsAttr <AuditableAttribute>() && !p.IsAttr <NotAuditableAttribute>());

                includedProperties.AddRange(props.Select(pi => pi.Name));
            }

            if (entry.State == EntityState.Modified)
            {
                var originalValues    = context.Entry(entry.Entity).GetDatabaseValues();
                var changedProperties = (from propertyName in originalValues.PropertyNames
                                         let propertyEntry = entry.Property(propertyName)
                                                             let currentValue = propertyEntry.CurrentValue
                                                                                let originalValue = originalValues[propertyName]
                                                                                                    where (!Equals(currentValue, originalValue) && includedProperties.Contains(propertyName))
                                                                                                    select new ChangedProperty
                {
                    Name = propertyName,
                    CurrentValue = currentValue,
                    OriginalValue = originalValue
                }).ToArray();

                if (changedProperties.Any())
                {
                    foreach (var log in changedProperties.Select(changedProperty => new AuditLog
                    {
                        Created = DateTime.Now,
                        EntityFullName = entry.Entity.GetType().FullName,
                        Entity = Utils.Serialize(entry.Entity),
                        EntityId = entityKey,
                        Operation = logOperation,
                        User = user,
                        OldValue = Convert.ToString(changedProperty.OriginalValue),
                        NewValue = Convert.ToString(changedProperty.CurrentValue),
                        PropertyName = changedProperty.Name
                    }))
                    {
                        // ReSharper disable once SuspiciousTypeConversion.Global
                        ((IAuditDbContext)context).AuditLogs.Add(log);
                    }
                }
            }
            else if (includedProperties.Any())
            {
                var log = new AuditLog
                {
                    Created        = DateTime.Now,
                    EntityFullName = entry.Entity.GetType().FullName,
                    Entity         = Utils.Serialize(entry.Entity),
                    EntityId       = entityKey,
                    Operation      = logOperation,
                    User           = user
                };

                // ReSharper disable once SuspiciousTypeConversion.Global
                ((IAuditDbContext)context).AuditLogs.Add(log);
            }
        }
コード例 #3
0
ファイル: LogContext.cs プロジェクト: bjornen/EF.Audit
        public void ApplyAuditLog(DbContext workingContext, DbEntityEntry entry, LogOperation logOperation)
        {
            var includedProperties = new List<string>();
            var entityKey = workingContext.GetEntityKey(entry.Entity);
            var entityType = entry.Entity.GetType();

            if (entry.IsAttr<AuditableAttribute>())
            {
                var props = entityType.GetProperties().Where(pi => !pi.IsAttr<NotAuditableAttrubute>());
                includedProperties.AddRange(props.Select(pi => pi.Name));
            }
            else
            {
                var props = entityType.GetProperties()
                    .Where(p => p.IsAttr<AuditableAttribute>() && !p.IsAttr<NotAuditableAttrubute>());

                includedProperties.AddRange(props.Select(pi => pi.Name));
            }

            if (entry.State == EntityState.Modified)
            {
                var originalValues = workingContext.Entry(entry.Entity).GetDatabaseValues();
                var changedProperties = (from propertyName in originalValues.PropertyNames
                    let propertyEntry = entry.Property(propertyName)
                    let currentValue = propertyEntry.CurrentValue
                    let originalValue = originalValues[propertyName]
                    where (!Equals(currentValue, originalValue) && includedProperties.Contains(propertyName))
                    select new ChangedProperty
                    {
                        Name = propertyName,
                        CurrentValue = currentValue,
                        OriginalValue = originalValue
                    }).ToArray();

                if (changedProperties.Any())
                {
                    foreach (var log in changedProperties.Select(changedProperty => new AuditLog
                    {
                        Created = DateTime.Now,
                        EntityFullName = entry.Entity.GetType().FullName,
                        Entity = Utils.Serialize(entry.Entity),
                        EntityIdBytes = Utils.Serialize(entry.Entity),
                        Operation = logOperation,
                        OldValue = changedProperty.OriginalValue.ToString(),
                        NewValue = changedProperty.CurrentValue.ToString(),
                        PropertyName = changedProperty.Name
                    }))
                    {
                        AuditLogs.Add(log);
                    }

                    SaveChanges();
                }
            }
            else
            {
                var log = new AuditLog
                {
                    Created = DateTime.Now,
                    EntityFullName = entry.Entity.GetType().FullName,
                    Entity = Utils.Serialize(entry.Entity),
                    EntityIdBytes = Utils.Serialize(entityKey),
                    Operation = logOperation,
                };

                AuditLogs.Add(log);
                SaveChanges();
            }
        }