Coder Social home page Coder Social logo

Comments (5)

thepirat000 avatar thepirat000 commented on June 3, 2024 1

This was fixed in version 20.2.4, please upgrade your references and re-test

from audit.net.

thepirat000 avatar thepirat000 commented on June 3, 2024

Please share your configuration (Audit.Core.Configuration.* and Audit.EntityFramework.Configuration.* calls) or share the code that reproduces the error

from audit.net.

thepirat000 avatar thepirat000 commented on June 3, 2024

From the stack trace I can see you're using the EF data provider to save the audits and you save the audits within the same DbContext. So you're probably letting the default behavior of using the same DbContext instance for saving the audits.

But whenever the SaveChanges fails, the DbContext instance will be set to a faulted state or be disposed, so that's why the audit throws the exception.

You could change the EF Data Provider configuration to use a different instance of your DbContext for the audit, so any failure in the save operation will not affect the audit. For this, you must provide a way to instantiate your audit DbContext.

Some examples:

// Resolve using a parameterless constructor 
Audit.Core.Configuration.Setup()
    .UseEntityFramework(ef => ef
        .UseDbContext<StammdatenDbContext>()
        ...

// Resolve from IServiceProvider (i.e. your project is ASP.NET core MVC)
Audit.Core.Configuration.Setup()
    .UseEntityFramework(ef => ef
        .UseDbContext(ev => ev.EntityFrameworkEvent.GetDbContext().GetService<StammdatenDbContext>())
        ...

from audit.net.

lucax88x avatar lucax88x commented on June 3, 2024

I'm not using UseEntityFramework extension, but UsePostgreSql extension instead, so my expectation is that it should use the npgsql driver to write audit stuffs and not the dbcontext audit. Does it make sense what I'm saying?

My configuration:

            Configuration
                .Setup()
                .UsePostgreSql(
                    config => config
                        .ConnectionString(dbConnectionString)
                        .Schema(Schema)
                        .TableName(Table)
                        .IdColumnName(IdColumn)
                        .DataColumn(DataColumn, DataType.JSONB)
                        .LastUpdatedColumnName(UpdatedDateColumn)
                );

how I add the interceptor:

    public static DbContextOptionsBuilder AddMyAuditTrail(this DbContextOptionsBuilder dbContextOptionsBuilder) =>
        dbContextOptionsBuilder.AddInterceptors(new AuditSaveChangesInterceptor());

// somewhere else
        services
            .AddPooledDbContextFactory<TD>(
                builder => {
                    if (addAuditTrail) {
                        builder.AddMyAuditTrail();
                    }

                    builder.UseNpgsql(
                            configuration.GetConnectionString(connectionStringKey),
                            optionsBuilder => {
                                optionsBuilder.EnableRetryOnFailure();
                            }
                        )
                        .UseLowerCaseNamingConvention();
                }
            );

How I create tables (by hosted service):

    public static async Task CreateTables(IConfiguration configuration) {
        var dbConnectionString = configuration.GetConnectionString(ConnectionStringKey);

        if (!string.IsNullOrEmpty(dbConnectionString)) {
            await using var connection = new NpgsqlConnection(dbConnectionString);
            await connection.OpenAsync();
            
            await using var createSchemaCommand = new NpgsqlCommand(
                $"CREATE SCHEMA IF NOT EXISTS {Schema}",
                connection
            );
            await createSchemaCommand.ExecuteNonQueryAsync();

            await using var createTableCommand = new NpgsqlCommand(
                @$"CREATE TABLE IF NOT EXISTS {Schema}.{Table} (
    {IdColumn} bigserial NOT NULL,
    {InsertedDateColumn} timestamp without time zone NOT NULL DEFAULT now(),
    {UpdatedDateColumn} timestamp without time zone NOT NULL DEFAULT now(),
    {DataColumn} jsonb NOT NULL,
    CONSTRAINT {Table}_pkey PRIMARY KEY (id) 
)",
                connection
            );
            await createTableCommand.ExecuteNonQueryAsync();
        }
        else {
            Log.Warning("No AuditTrail connection string found, not using any audit trail!");
        }
    }

from audit.net.

thepirat000 avatar thepirat000 commented on June 3, 2024

You're right, in that case, I think the audit should not throw.

I would work on the fix to allow continuing with the audit process even if the DbContext got disposed of within the SaveChanges operation.

from audit.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.