实施审计跟踪c#
我为服务器中的每个操作(添加、修改和删除)实现了一个审计日志。问题发生在修改的属性中,因为我审核了所有被修改的属性,但有些属性我不想审核。例如:时间戳或其他。 我就是这么做的,效果很好: 1) 我在DBContext中创建了另一个SaveChanges()方法 (二)实施审计跟踪c#,c#,.net,audit-trail,C#,.net,Audit Trail,我为服务器中的每个操作(添加、修改和删除)实现了一个审计日志。问题发生在修改的属性中,因为我审核了所有被修改的属性,但有些属性我不想审核。例如:时间戳或其他。 我就是这么做的,效果很好: 1) 我在DBContext中创建了另一个SaveChanges()方法 (二) if(dbEntity.State==EntityState.Modified) { foreach(dbEntity.OriginalValues.PropertyNames中的字符串propertyName) { 如果(!Eq
if(dbEntity.State==EntityState.Modified)
{
foreach(dbEntity.OriginalValues.PropertyNames中的字符串propertyName)
{
如果(!Equals(dbEntity.OriginalValues.GetValue(propertyName),dbEntity.CurrentValues.GetValue(propertyName)))
{
var log=新的AuditLogDetailEntity()
{
时间戳=时间戳,
Type=“M”,//已修改
EntityName=tableName1,
PrimaryKeyValue=Convert.ToInt32(dbEntity.CurrentValues.GetValue(primaryKeyName)),
PropertyName=PropertyName,
OldValue=dbEntity.OriginalValues.GetValue(propertyName)==null?null:dbEntity.OriginalValues.GetValue(propertyName).ToString(),
NewValue=dbEntity.CurrentValues.GetValue(propertyName)==null?null:dbEntity.CurrentValues.GetValue(propertyName).ToString()
};
changesCollection.Add(日志);
}
}
}`
这是一个提取代码,不是所有函数。
我可以在内部进行验证,询问我不想审核的字段,但是,有没有更彻底的方法?可能会在类中添加一些数据注释,或者其他东西。。
谢谢。您可以使用[System.ComponentModel.DataAnnotations.Schema.NotMapped]属性进行此操作。您可以使用[System.ComponentModel.DataAnnotations.Schema.NotMapped]属性进行此操作。您可以创建自定义属性
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class UnMappedAttribute : Attribute
{
}
然后检查每个属性是否都有
foreach (string propertyName in dbEntity.OriginalValues.PropertyNames)
{
if(!dbEntity.Entity.GetType().GetCustomAttributes(typeof(UnMappedAttribute), true).Any())
{
continue;
}
if (!Equals(dbEntity.OriginalValues.GetValue<object>(propertyName), dbEntity.CurrentValues.GetValue<object>(propertyName)))
{
//.....
}
}
foreach(dbEntity.OriginalValues.PropertyNames中的字符串propertyName)
{
if(!dbEntity.Entity.GetType().GetCustomAttributes(typeof(unappedAttribute),true).Any())
{
继续;
}
如果(!Equals(dbEntity.OriginalValues.GetValue(propertyName),dbEntity.CurrentValues.GetValue(propertyName)))
{
//.....
}
}
您可以创建自定义属性
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class UnMappedAttribute : Attribute
{
}
然后检查每个属性是否都有
foreach (string propertyName in dbEntity.OriginalValues.PropertyNames)
{
if(!dbEntity.Entity.GetType().GetCustomAttributes(typeof(UnMappedAttribute), true).Any())
{
continue;
}
if (!Equals(dbEntity.OriginalValues.GetValue<object>(propertyName), dbEntity.CurrentValues.GetValue<object>(propertyName)))
{
//.....
}
}
foreach(dbEntity.OriginalValues.PropertyNames中的字符串propertyName)
{
if(!dbEntity.Entity.GetType().GetCustomAttributes(typeof(unappedAttribute),true).Any())
{
继续;
}
如果(!Equals(dbEntity.OriginalValues.GetValue(propertyName),dbEntity.CurrentValues.GetValue(propertyName)))
{
//.....
}
}
怎么做?我应该使用哪个数据注释?感谢您这样使用它:类AuditLogDetailEntity{[System.ComponentModel.DataAnnotations.Schema.NotMapped]DateTime时间戳{get;set;}//etc}如何?我应该使用哪个数据注释?感谢您这样使用它:类AuditLogDetailEntity{[System.ComponentModel.DataAnnotations.Schema.NotMapped]DateTime时间戳{get;set;}//etc}