コード例 #1
0
        /// <summary>
        /// 日志拦截写入
        /// </summary>
        internal void WriteAuditLog()
        {
            if (this.AuditLogger == null)
            {
                return;
            }

            foreach (var dbEntry in this.ChangeTracker.Entries <ModelBase>().Where(p => p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified))
            {
                AuditableAttribute _auditableAttr = dbEntry.Entity.GetType().GetTypeInfo().GetCustomAttribute <AuditableAttribute>();

                if (_auditableAttr == null)
                {
                    continue;
                }

                //string _operaterName = ServiceCallContext.Current.Operater.Name;
                //Task.Factory.StartNew(() =>
                //{
                //    TableAttribute _tableAttr = dbEntry.Entity.GetType().GetTypeInfo().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;
                //    string _tableName = _tableAttr != null ? _tableAttr.Name : dbEntry.Entity.GetType().Name;
                //    string _moduleName = dbEntry.Entity.GetType().FullName.Split('.').Skip(1).FirstOrDefault();
                //    this.AuditLogger.WriteLog(dbEntry.Entity.ID, _operaterName, _moduleName, _tableName, dbEntry.State.ToString(), dbEntry.Entity);
                //});
            }
        }
コード例 #2
0
        private void DbContextBase_DataChangedEvent(Type type, string sql)
        {
            if (type != null)
            {
                Type _entityType = type;
                AuditableAttribute _auditableAttr = _entityType.GetCustomAttributes(typeof(AuditableAttribute), false).SingleOrDefault() as AuditableAttribute;

                if (_auditableAttr == null)
                {
                    return;
                }

                int    _userId           = ServiceCallContext.Current.Operater.UserId;
                Guid   _userGuid         = ServiceCallContext.Current.Operater.UserGuid;
                string _userBase64String = ServiceCallContext.Current.Operater.UserBase64;
                Thread _task             = new Thread(() =>
                {
                    var _tableAttr    = _entityType.GetCustomAttributes(typeof(TableNameAttribute), true);
                    string _tableName = _tableAttr.Length == 0 ? _entityType.Name : (_tableAttr[0] as TableNameAttribute).Value;
                    this.AuditLogger.WriteLog(_userId, _userGuid, _userBase64String, _tableName, sql, DateTime.UtcNow);
                });
                _task.Start();
            }
        }
コード例 #3
0
ファイル: Extensions.cs プロジェクト: mohocp/EFCore.Audit
        internal static bool IsAuditable(this EntityEntry entityEntry)
        {
            AuditableAttribute enableAuditAttribute = (AuditableAttribute)Attribute.GetCustomAttribute(entityEntry.Entity.GetType(), typeof(AuditableAttribute));

            return(enableAuditAttribute != null);
        }
コード例 #4
0
        /// <summary>
        /// Saves state change in Audit SQL-table
        /// </summary>
        private void Logging()
        {
            foreach (DbEntityEntry item in database.ChangeTracker.Entries())
            {
                System.Type        itemType           = item.State == EntityState.Deleted ? item.OriginalValues.ToObject().GetType() : item.CurrentValues.ToObject().GetType();
                AuditableAttribute auditableAttribute = itemType.GetCustomAttribute <AuditableAttribute>();

                switch (item.State)
                {
                case EntityState.Added:
                case EntityState.Deleted:
                    switch (auditableAttribute.AuditScope)
                    {
                    case AuditScope.ClassOnly:
                    case AuditScope.ClassAndProperties:
                        var describable = item.Entity as IDescribable;
                        if (describable != null)
                        {
                            dynamic auditLog = new JObject();
                            auditLog.State = item.State.ToString();
                            auditLog.Data  = describable.Describe();
                            Audit audit = new Audit
                            {
                                EntityName   = item.Entity.GetType().Name,
                                Logs         = auditLog.ToString(),
                                ModifiedDate = DateTime.Now,
                                ModifiedBy   = Environment.UserDomainName != null ? $"{Environment.UserName}@{Environment.UserDomainName}" : $"{Environment.UserName}",
                                ModifiedFrom = Environment.MachineName
                            };
                            Audit.Create(audit);
                        }
                        break;

                    default:
                        continue;
                    }
                    break;

                case EntityState.Modified:
                    switch (auditableAttribute.AuditScope)
                    {
                    case AuditScope.PropertiesOnly:
                    case AuditScope.ClassAndProperties:
                        foreach (string propertyName in item.OriginalValues.PropertyNames)
                        {
                            var originalValue = item.GetDatabaseValues().GetValue <object>(propertyName);
                            var currentValue  = item.CurrentValues.GetValue <object>(propertyName);
                            if (!Equals(originalValue, currentValue))
                            {
                                dynamic auditLog = new JObject();
                                auditLog.State         = item.State.ToString();
                                auditLog.PropertyName  = propertyName;
                                auditLog.ID            = item.GetDatabaseValues().GetValue <object>("ID");
                                auditLog.OriginalValue = originalValue;
                                auditLog.NewValue      = currentValue;
                                Audit audit = new Audit
                                {
                                    EntityName   = item.Entity.GetType().Name,
                                    Logs         = auditLog.ToString(),
                                    ModifiedDate = DateTime.Now,
                                    ModifiedBy   = Environment.UserDomainName != null ? $"{Environment.UserName}@{Environment.UserDomainName}" : $"{Environment.UserName}",
                                    ModifiedFrom = Environment.MachineName
                                };
                                Audit.Create(audit);
                            }
                        }
                        break;

                    default:
                        continue;
                    }
                    break;

                //In case that item.State equals EntityState.DETACHED and EntityState.UNCHANGED
                default:
                    continue;
                }
            }
        }