![](/img/trans.png)
[英]How to log Update<T>(IList<T>) action as multiple audit records using Audit.net
[英]Audit.Net on Update inserts empty records for every value that is in the table
我正在使用 Audit.Net EntityFramework 数据提供程序。 这是我在 Startup.cs 中的配置
Audit.Core.Configuration.Setup().UseEntityFramework(_ => _.AuditTypeMapper(t => typeof(AuditLog)).AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.AuditDate = DateTime.Now;
entity.AuditUser = Environment.UserName;
// entity.AuditUsername = Environment.MachineName;
entity.AuditUsername = HttpContext.Current.User.Identity.Name;
})
.IgnoreMatchedProperties(true));
这是 DBContext
public class DbContext : Audit.EntityFramework.AuditDbContext, IDbContext
{
public DbContext()
: base("DbContext")
{
Database.SetInitializer<DbContext>(null);
}
public Database GetDatabase()
{
return this.Database;
}
public new DbEntityEntry<T> Entry<T>(T entity) where T : class
{
return (new PContext().Entry(entity) as DbEntityEntry<T>);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
仅当我更改表单或数据表上的值时才会出现此问题
突出显示的是更新的审计建议。 添加以下所有内容,似乎它进入了某种递归,因为 Update rec 后的第一个 JSON 记录有 1281 个字符,第二个 1146 ,第三个 1763 ,第 4 个 2773 等等,直到我停止项目。
在entity.AuditData = entry.ToJson();
得到这个
System.OutOfMemoryException: 'Exception of type 'System.OutOfMemoryException' was thrown.'
可能是什么问题?
如果您直接从已审计的 DbContext 对AuditLog
DbSet 进行更改,则应将AuditLog
实体标记为不可审计,以便审计库不会获取更改。
这可以通过使用[AuditIgnore]
装饰类来完成,例如:
[AuditIgnore]
public class AuditLog
{
...
}
或者通过配置:
Audit.EntityFramework.Configuration.Setup()
.ForContext<P121DbContext>()
.UseOptOut()
.Ignore<AuditLog>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.