public static Task LogDelete(IEntity entity) { if (entity is IAuditEvent) { return(Task.CompletedTask); } if (!Config.Get <bool>("Database:Audit:Delete:Action")) { return(Task.CompletedTask); } if (!LogEventsAttribute.ShouldLog(entity.GetType())) { return(Task.CompletedTask); } var data = string.Empty; if (Config.Get("Database:Audit:Delete:Data", defaultValue: true)) { var changes = Context.Current.Database() .GetProvider(entity.GetType()).GetUpdatedValues(entity, null); data = EntityProcessor.ToChangeXml(changes); } return(Log("Delete", data, entity)); }
public static async Task LogUpdate(IEntity entity) { if (entity is IAuditEvent) { return; } if (!Config.Get <bool>("Database:Audit:Update:Action")) { return; } if (!LogEventsAttribute.ShouldLog(entity.GetType())) { return; } var data = string.Empty; if (Config.Get("Database:Audit:Update:Data", defaultValue: true)) { data = await EntityProcessor.GetChangesXml(entity); if (data.IsEmpty()) { return; // No changes have happened, ignore recording the action: } } await Log("Update", data, entity); }
/// <summary> /// Gets the data of a specified object's properties in a dictionary. /// </summary> public static Dictionary <string, string> GetDataToLog(IEntity entity) { var result = new Dictionary <string, string>(); var type = entity.GetType(); var propertyNames = type.GetProperties().Select(p => p.Name).Distinct().Trim().ToArray(); Func <string, PropertyInfo> getProperty = name => type.GetProperties() .Except(p => p.IsSpecialName) .Except(p => p.GetGetMethod().IsStatic) .Except(p => p.Name == "ID") .Where(p => p.GetSetMethod() != null && p.GetGetMethod().IsPublic) .OrderByDescending(x => x.DeclaringType == type) .FirstOrDefault(p => p.Name == name); var dataProperties = propertyNames.Select(getProperty).ExceptNull() .Except(x => CalculatedAttribute.IsCalculated(x)) .Where(x => LogEventsAttribute.ShouldLog(x)) .ToArray(); foreach (var p in dataProperties) { var propertyType = p.PropertyType; string propertyValue; try { if (propertyType == typeof(IList <Guid>)) { propertyValue = (p.GetValue(entity) as IList <Guid>).ToString(","); } else if (propertyType.IsGenericType) { propertyValue = (p.GetValue(entity) as IEnumerable <object>).ToString(", "); } else { propertyValue = p.GetValue(entity).ToStringOrEmpty(); } if (propertyValue.IsEmpty()) { continue; } } catch { // No log needed continue; } result.Add(p.Name, propertyValue); } return(result); }
public static Task LogInsert(IEntity entity) { if (entity is IAuditEvent) { return(Task.CompletedTask); } if (!Config.Get <bool>("Database:Audit:Insert:Action")) { return(Task.CompletedTask); } if (!LogEventsAttribute.ShouldLog(entity.GetType())) { return(Task.CompletedTask); } var data = string.Empty; if (Config.Get("Database:Audit:Insert:Data", defaultValue: true)) { data = EntityProcessor.GetDataXml(entity); } return(Log("Insert", data, entity)); }
public IDictionary <string, Tuple <string, string> > GetUpdatedValues(IEntity original, IEntity updated) { if (original == null) { throw new ArgumentNullException(nameof(original)); } var result = new Dictionary <string, Tuple <string, string> >(); var type = original.GetType(); var propertyNames = type.GetProperties().Distinct().Select(p => p.Name.Trim()).ToArray(); PropertyInfo getProperty(string name) { return(type.GetProperties() .Except(p => p.IsSpecialName || p.GetGetMethod().IsStatic) .Where(p => p.GetSetMethod() != null && p.GetGetMethod().IsPublic) .OrderByDescending(x => x.DeclaringType == type) .FirstOrDefault(p => p.Name == name)); } var dataProperties = propertyNames.Select(getProperty).ExceptNull() .Except(x => CalculatedAttribute.IsCalculated(x)) .Where(x => LogEventsAttribute.ShouldLog(x)) .ToArray(); foreach (var p in dataProperties) { var propertyType = p.PropertyType; // Get the original value: string originalValue, updatedValue = null; if (propertyType == typeof(IList <Guid>)) { try { originalValue = (p.GetValue(original) as IList <Guid>).ToString(","); if (updated != null) { updatedValue = (p.GetValue(updated) as IList <Guid>).ToString(","); } } catch { // No logging is needed. continue; } } else if (propertyType.IsGenericType) { try { originalValue = (p.GetValue(original) as IEnumerable <object>).ToString(", "); if (updated != null) { updatedValue = (p.GetValue(updated) as IEnumerable <object>).ToString(", "); } } catch { // No logging is needed. continue; } } else { try { originalValue = $"{p.GetValue(original)}"; if (updated != null) { updatedValue = $"{p.GetValue(updated)}"; } } catch { // No logging is needed continue; } } if (updated == null || originalValue != updatedValue) { if (result.LacksKey(p.Name)) { result.Add(p.Name, new Tuple <string, string>(originalValue, updatedValue)); } } } return(result); }