Audit.EntityFramework.Core how to store changes to multiple tables without creating audit objects

73 Views Asked by At

iam using Audit.EntityFramework.Core library to setup audit log in my new application.

Following guide i setup my dbContext (see below code) but from what i can understand it seems that i have to create in my domain a specific object for every real object i want to log but honestly they have no sense in a DDD perspective.Moreover i see no example of how these objects should be

Is there a way to tell library simply the name of the tables where to store logs, like for example what i did with Envers (library for audit log NHibernate)?

public class DatabaseContext : AuditDbContext, IDatabaseContext
{
    private readonly ILoggerFactory _loggerFactory;

    #region Ctor
    public DatabaseContext(DbContextOptions<DatabaseContext> options, ILoggerFactory loggerFactory)
     : base(options)
    {
        _loggerFactory = loggerFactory;
        //this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        this.ChangeTracker.LazyLoadingEnabled = false;

        Audit.EntityFramework.Configuration.Setup()
            .ForContext<DatabaseContext>(config => config
                .IncludeEntityObjects()
                .AuditEventType("{context}:{database}"))
            .UseOptOut()
                .IgnoreAny(t => t.Name.EndsWith("History"));


        Audit.Core.Configuration.Setup()
        .UseEntityFramework(x => x
        .UseDbContext<DatabaseContext>()
        .AuditTypeNameMapper(typeName => "Audit_" + typeName)
        .AuditEntityAction((ev, ent, auditEntity) =>
        {
            // auditEntity is object
            ((dynamic)auditEntity).AuditDate = DateTime.UtcNow;
        }));
    }
    #endregion
    #region DbSet
    public DbSet<ParametroConfigurazione> ParametriConfigurazione { get; set; }
    #endregion
    #region Methods
    public Task<int> SaveChangesAsync() => base.SaveChangesAsync();
    #endregion

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ParametroConfigurazioneConfiguration).Assembly);


        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseLoggerFactory(_loggerFactory)
            .AddInterceptors(new AuditCommandInterceptor()
            {
                ExcludeNonQueryEvents = true,
                AuditEventType = "DatabaseContext",
                IncludeReaderResults = true
            })
            ;
    }

}
0

There are 0 best solutions below