Comments (5)
This was fixed in version 20.2.4, please upgrade your references and re-test
from audit.net.
Please share your configuration (Audit.Core.Configuration.*
and Audit.EntityFramework.Configuration.*
calls) or share the code that reproduces the error
from audit.net.
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.
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.
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)
- Override feature doesn't work. HOT 2
- EventEntry.EntityType is null HOT 2
- If EF Core configures different column name for a entity property , the corresponding value of audit property is null HOT 2
- I need help with EF Core HOT 9
- UseElasticsearch entity update issue HOT 8
- Is there a UI code to show these logs? HOT 1
- ElasticSearch provider create json file in working directory HOT 8
- Migration doesn't create Audit_ tables HOT 4
- Support CancellationToken on all async methods HOT 3
- I get a missing method Exception when saving HOT 11
- Response content not capturing correctly when Exception Handlers is used HOT 1
- [AuditApi] - override middleware settings with controller attributes HOT 3
- Error on inserting Audit Data HOT 2
- EF Core value updates not audited HOT 3
- EF Core value updates are not audited HOT 13
- Custom field empty in Entries Array HOT 3
- Bump Microsoft.Bcl.AsyncInterfaces HOT 1
- Limited configuration of MongoDB provider HOT 1
- Related object causes multiple audit entries HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from audit.net.