1

I am using Audit.Net EntityFramework data provider. This is my configuration in 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));

This is 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>();
    }

The problem only occurs when I change the value on a form or data table

The highlighted is the updated audit rec. All below are added and it seems it enters some kind of recursion since the first JSON record after the Update rec has 1281 characters, second 1146 , third 1763 ,4-th 2773 and so on until I stop the project.

at entity.AuditData = entry.ToJson(); getting this

System.OutOfMemoryException: 'Exception of type 'System.OutOfMemoryException' was thrown.'

What might be the problem?

nightowl
  • 309
  • 1
  • 3
  • 13

1 Answers1

1

If you are making changes to your AuditLog DbSet directly from your audited DbContext, you should mark the AuditLog entity as not auditable so the changes are not picked up by the audit library.

This can be done by decorating the class with [AuditIgnore] for example:

[AuditIgnore]
public class AuditLog
{
    ...
}

Or via configuration:

Audit.EntityFramework.Configuration.Setup()
    .ForContext<P121DbContext>()
    .UseOptOut()
    .Ignore<AuditLog>();
thepirat000
  • 12,362
  • 4
  • 46
  • 72