Good afternoon. I tried to use this library in my Asp.Net Core application. I need to collect logs from my Web.Api app (via audit.net.webapi), and from the server (audit.net). To do this, I created a custom provider, in which, through DI asp.net.core I inject the implementation of a Repository that can work with the database via EF:
private readonly IMapper _mapper;
private readonly AuditLogRepository _repository;
publicAuditLogDataProvider(
imapper mapper,
AuditLogRepository repository)
{
_mapper = mapper;
_repository = repository;
}
In the InsertEventAsync method, I make the code I need and call the repository methods to add the log to the database.
The repository uses the Ef DB Context, which is also injected through DI:
private readonly AuditLogDbContext _dbContext;
public AuditLogRepository(AuditLogDbContext context)
{
_dbContext = context;
}
When calling any method, the DB Context will fail with the error "Cannot access a disposed context instance error".
After a short digging in the repository, I found this line in AuditApiAdapter.Core.AfterExecutedAsync:
await auditScope.DisposeAsync();
I think this is the reason for calling Dispose on the DB Context before it had time to be called.
Is there any plan to support DI in the future? Or is there some other way to avoid this error?
Audit.net setup:
Program.cs:
Audit.Core.Configuration.Setup()
.UseCustomProvider(services.GetRequiredService<AuditDataProvider>())
.WithCreationPolicy(EventCreationPolicy.InsertOnStartReplaceOnEnd);
Startup.cs
services.AddScoped<AuditDataProvider, AuditLogDataProvider>();
services.AddMvc(mvc =>
{
mvc.AddAuditFilter(config =>
config.LogAllActions()
.IncludeHeaders()
.IncludeModelState()
.IncludeRequestBody()
.IncludeResponseBody()
.IncludeResponseHeaders());
});