Vinicius Teixeira
Vinicius Teixeira

Reputation: 41

Audi.EntityFramework does not generate log

I want to generate logs of multiple entities for just one table.

At startup I'm configuring dbcontext this way

         Audit.Core.Configuration.Setup()
          .UseEntityFramework(_ => _
        .AuditTypeMapper(t => typeof(AuditLog))
        .AuditEntityAction<AuditLog>((ev, entry, entity) =>
        {
            entity.AuditData = entry.ToJson();
            entity.EntityType = entry.EntityType.Name;
            entity.AuditDate = DateTime.Now;
            entity.AuditUser = Environment.UserName;
            entity.TablePk = entry.PrimaryKey.First().Value.ToString();
        })
    .IgnoreMatchedProperties(true));
        }

I created an entity with these properties and generated it in the database

And using this Save Changes override approach

public class MyContext : DbContext
{
    private readonly DbContextHelper _helper = new DbContextHelper();
    private readonly IAuditDbContext _auditContext;

    public MyContext(DbContextOptions<MyContext> options) : base(options)
    {
        _auditContext = new DefaultAuditContext(this);
        _helper.SetConfig(_auditContext);
    }

  public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        return await _helper.SaveChangesAsync(_auditContext, () => base.SaveChangesAsync(cancellationToken));
    }

But even so no log records are generated.

Is something missing from the configuration?

I'm using MySQL with EF Core

Upvotes: 2

Views: 65

Answers (1)

thepirat000
thepirat000

Reputation: 13114

You are not overriding the synchronous int SaveChanges() method as shown here on the readme, and probably you are saving changes synchronously.

public override int SaveChanges()
{
    return _helper.SaveChanges(_auditContext, () => base.SaveChanges());
}

Upvotes: 1

Related Questions