// Determines whether to include the entity on the audit log or not private bool IncludeEntity(IAuditDbContext context, object entity, AuditOptionMode mode) { var type = entity.GetType(); #if EF_FULL type = ObjectContext.GetObjectType(type); #else if (type.FullName.StartsWith("Castle.Proxies.")) { type = type.GetTypeInfo().BaseType; } #endif bool?result = EnsureEntitiesIncludeIgnoreAttrCache(type); //true:excluded false=ignored null=unknown if (result == null) { // No static attributes, check the filters var localConfig = EntityFramework.Configuration.GetConfigForType(context.GetType()); var globalConfig = EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext)); var included = EvalIncludeFilter(type, localConfig, globalConfig); var ignored = EvalIgnoreFilter(type, localConfig, globalConfig); result = included ? true : ignored ? false : (bool?)null; } if (mode == AuditOptionMode.OptIn) { // Include only explicitly included entities return(result.GetValueOrDefault()); } // Include all, except the explicitly ignored entities return(result == null || result.Value); }
/// <summary> /// Creates the Audit Event. /// </summary> /// <param name="includeEntities">To indicate if it must incluide the serialized entities.</param> /// <param name="mode">The option mode to include/exclude entities from Audit.</param> public EntityFrameworkEvent CreateAuditEvent(bool includeEntities, AuditOptionMode mode) { var modifiedEntries = GetModifiedEntries(mode); if (modifiedEntries.Count == 0) { return(null); } var clientConnectionId = GetClientConnectionId(Database.Connection); var efEvent = new EntityFrameworkEvent() { Entries = new List <EventEntry>(), Database = Database.Connection.Database, ConnectionId = clientConnectionId, TransactionId = GetCurrentTransactionId(clientConnectionId) }; foreach (var entry in modifiedEntries) { var entity = entry.Entity; var validationResults = entry.GetValidationResult(); efEvent.Entries.Add(new EventEntry() { Valid = validationResults.IsValid, ValidationResults = validationResults.ValidationErrors.Select(x => x.ErrorMessage).ToList(), Entity = includeEntities ? entity : null, Action = GetStateName(entry.State), Changes = entry.State == EntityState.Modified ? GetChanges(entry) : null, ColumnValues = GetColumnValues(entry), PrimaryKey = GetPrimaryKey(entry), Table = GetEntityName(entity) }); } return(efEvent); }
private bool IncludeEntity(IAuditDbContext context, DbEntityEntry entry, AuditOptionMode mode) #endif { var type = entry.Entity.GetType(); #if NET45 type = ObjectContext.GetObjectType(type); #endif bool?result = EnsureEntitiesIncludeIgnoreAttrCache(type); //true:excluded false=ignored null=unknown if (result == null) { // No static attributes, check the filters var localConfig = EntityFramework.Configuration.GetConfigForType(context.GetType()); var globalConfig = EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext)); var included = EvalIncludeFilter(type, localConfig, globalConfig); var ignored = EvalIgnoreFilter(type, localConfig, globalConfig); result = included ? true : ignored ? false : (bool?)null; } if (mode == AuditOptionMode.OptIn) { // Include only explicitly included entities return(result.GetValueOrDefault()); } // Include all, except the explicitly ignored entities return(result == null || result.Value); }
private List <DbEntityEntry> GetModifiedEntries(AuditOptionMode mode) #endif { return(ChangeTracker.Entries() .Where(x => x.State != EntityState.Unchanged && x.State != EntityState.Detached && IncludeEntity(x, mode)) .ToList()); }
private bool IncludeEntity(DbEntityEntry entry, AuditOptionMode mode) #endif { var type = entry.Entity.GetType(); if (!EntitiesIncludedCache.ContainsKey(type)) { var includeAttr = type.GetTypeInfo().GetCustomAttribute(typeof(AuditIncludeAttribute)); if (includeAttr != null) { EntitiesIncludedCache[type] = true; // Type Included by IncludeAttribute } else { var ignoreAttr = type.GetTypeInfo().GetCustomAttribute(typeof(AuditIgnoreAttribute)); if (ignoreAttr != null) { EntitiesIncludedCache[type] = false; // Type Ignored by IgnoreAttribute } else { // No attribute specified, check the global config var localConfig = EntityFramework.Configuration.GetConfigForType(GetType()); var globalConfig = EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext)); var ignored = localConfig?.IgnoredTypes.Contains(type) ?? globalConfig?.IgnoredTypes.Contains(type) ?? false; var included = localConfig?.IncludedTypes.Contains(type) ?? globalConfig?.IncludedTypes.Contains(type) ?? false; EntitiesIncludedCache[type] = included ? true : ignored ? (bool?)false : null; } } } if (mode == AuditOptionMode.OptIn) { return(EntitiesIncludedCache[type] != null && EntitiesIncludedCache[type].Value); } return(EntitiesIncludedCache[type] == null || EntitiesIncludedCache[type].Value); }
internal static void SetMode <T>(AuditOptionMode mode) where T : AuditDbContext { EnsureConfigFor <T>().Mode = mode; }
/// <summary> /// Gets the modified entries to process. /// </summary> #if NETCOREAPP1_0 private List <EntityEntry> GetModifiedEntries(AuditOptionMode mode)
// Determines whether to include the entity on the audit log or not #if NETCOREAPP1_0 private bool IncludeEntity(EntityEntry entry, AuditOptionMode mode)
// Determines whether to include the entity on the audit log or not #if NETCOREAPP1_0 private bool IncludeEntity(IAuditDbContext context, EntityEntry entry, AuditOptionMode mode)
internal static void SetMode <T>(AuditOptionMode mode) { EnsureConfigFor <T>().Mode = mode; }