Ejemplo n.º 1
0
        public void Log(AbstractPreDatabaseOperationEvent @event,object[] newstate,object[] oldstate,string tableName, SqlConnection connection)
        {
            //if we are writing audit table - we dont log the information
            if(tableName == "[simpleaudit]" || tableName == "\"simpleaudit\"")
            {
                return;
            }

            var time = DateTime.Now;
            string user;
            if (HttpContext.Current != null)
            {
                user = HttpContext.Current.User.Identity.Name;
            }
            else
            {
                user = "******";
            }

            if (oldstate != null)
            {
                var dirties = @event.Persister.FindDirty(newstate, oldstate, @event.Entity, @event.Session);
                foreach (int dirty in dirties)
                {
                    var property = @event.Persister.PropertyNames[dirty];

                    var oldvalue = "null";
                    if (oldstate[dirty] != null)
                    {
                        oldvalue = oldstate[dirty].ToString();
                    }
                    var newvalue = "null";
                    if (newstate[dirty] != null)
                    {
                        newvalue = newstate[dirty].ToString();
                    }

                    SaveAudit(user, time, newvalue, oldvalue, property, tableName, connection);

                }
            }
            else
            {
                for(int i=0;i<newstate.Length;i++)
                {
                    object obj = newstate[i];
                    if (obj != null)
                    {
                        var newValue = obj.ToString();
                        var value = newValue.Substring(0, Math.Min(newValue.Length, 100));
                        var property = @event.Persister.PropertyNames[i];
                        SaveAudit(user, time, value, "no", property, tableName, connection);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 private void SetAuditProperties(AbstractPreDatabaseOperationEvent @event,  IEntityPersister persister, 
     object[] state, IUserAccount auditUser, DateTime auditDate)
 {
     foreach (var propertyPair in _auditProperties) {
     if (propertyPair.Key.Contains(ModelConstants.UPDATEDBY)) {
        propertyPair.Value.SetValue(@event.Entity, auditUser, null);
        Set(persister, state, propertyPair.Key, auditUser);
     }
     if (propertyPair.Key.Contains(ModelConstants.UPDATEDON)) {
        propertyPair.Value.SetValue(@event.Entity, auditDate, null);
        Set(persister, state, propertyPair.Key, auditDate);
     }
     if (propertyPair.Key.Contains(ModelConstants.CREATEDBY)) {
        var currentValue = (IUserAccount)propertyPair.Value.GetValue(@event.Entity, null);
        if(currentValue == null) {
           propertyPair.Value.SetValue(@event.Entity, auditUser, null);
           Set(persister, state, propertyPair.Key, auditUser);
        }
        else {
           propertyPair.Value.SetValue(@event.Entity, currentValue, null);
           Set(persister, state, propertyPair.Key, currentValue);
        }
     }
     if (propertyPair.Key.Contains(ModelConstants.CREATEDON)) {
        var currentValue = (DateTime?)propertyPair.Value.GetValue(@event.Entity, null);
        if(currentValue == null ||
           !((DateTime)currentValue).IsBetween(DateTime.Parse("1/1/1900"), DateTime.Parse("1/1/3000"))) {
           propertyPair.Value.SetValue(@event.Entity, auditDate, null);
           Set(persister, state, propertyPair.Key, auditDate);
        }
        else {
           propertyPair.Value.SetValue(@event.Entity, currentValue, null);
           Set(persister, state, propertyPair.Key, currentValue);
        }
     }
      }
 }