Comments (9)
Try replacing the .UseDbContext<ApplicationDbContext>()
line with this:
.UseDbContext(ev => ev.EntityFrameworkEvent.GetDbContext().GetService<ApplicationDbContext>())
So the ApplicationDbContext
instance is resolved from the IServiceProvicer
.
Another way is to supply the ApplicationDbContext
constructor parameters, in your case the DbContextOptions<AuditDbContext>
, for example:
.UseDbContext<ApplicationDbContext>(new DbContextOptionsBuilder<ApplicationDbContext>().UseSqlServer(connectionString).Options)
from audit.net.
Hi Federico
Thanks - it works. Your library is great.
But I have one more question.
I save all changes in the AuditLog table. Is it possible to save only the changed data values in a separate table, e.g. AuditLogChanges.
In this table I would like to save values from ChangeObject.
It would be good if the AuditLogVhanges table was related to the AuditLog table.
Is it even possible?
Bogdan
from audit.net.
Can you elaborate a little bit more? maybe sharing the auditlog / auditlogchanges schema you have in mind
from audit.net.
My current AuditLog class
`public class AuditLog
{
public Guid Id { get; set; }
public string? TableName { get; set; }
public string? TableName1 { get; set; }
public string? AuditAction { get; set; }
public DateTime AuditDate { get; set; }
public string? AuditUserName { get; set; }
public int Duration { get; set; }
public string? EventType { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string? Type { get; set; }
public string? DatabaseName { get; set; }
public string? Changes { get; set; }
}`
The planned AuditLogChanges class
public class AuditLogChanges { public Guid Id { get; set; } public Guid AuditLogId { get; set; } public string? ColumnName { get; set; } public string? OriginalValue { get; set; } public string? NewValue { get; set; } }
Related one-to-many AuditLog.Id -> AuditLogChanges.AuditLog.Id
My current configuration
`Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.UseDbContext(new DbContextOptionsBuilder().UseSqlServer(connectionString).Options)
.AuditTypeExplicitMapper(map => map
.Map<Players, AuditLog>((players, audit) =>
{
audit.TableName = "Players";
})
.Map<Positions, AuditLog>((players, audit) =>
{
audit.TableName = "Positions";
})
.AuditEntityAction<AuditLog>((evt, entry, audit) =>
{
audit.TableName1 = entry.Table.ToString();
audit.AuditDate = DateTime.UtcNow;
audit.AuditAction = entry.Action;
audit.AuditUserName = evt.Environment.UserName;
audit.Duration = evt.Duration;
audit.EventType = evt.EventType;
audit.StartDate = evt.StartDate;
audit.EndDate = evt.EndDate;
audit.Type = evt.EventType;
audit.DatabaseName = evt.GetEntityFrameworkEvent().Database;
audit.Changes = entry.ToJson();
}))
.IgnoreMatchedProperties(true));`
Ultimately, I would like to create an audit manager in which I could show the changed values in individual columns of the table.
from audit.net.
A new problem
I am getting the following error while updating a record in the database
Bogdan
from audit.net.
Not sure if I'm understanding your use case, but assuming you want to audit entities to two tables; one for the table audit and another one for the column values, then you can use the AuditTypeMapper with a common entity action.
For example, I'm simplifying your audit tables to something like this:
// This table stores an action performed to a table (insert, update or delete)
[AuditIgnore]
public class AuditLogTable
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
public string User { get; set; }
public string Table { get; set; }
public string Action { get; set; }
public ICollection<AuditLogColumn> Columns { get; set; }
}
// This table stores the column values for the AuditLogTable entity
[AuditIgnore]
public class AuditLogColumn
{
[Key]
public int Id { get; set; }
public int AuditLogTableId { get; set; }
public string Column { get; set; }
public string OldValue { get; set; }
public string NewValue { get; set; }
}
Note the [AuditIgnore]
attribute for the audit entities, this is to avoid auditing the audit table and generating an infinite loop.
So you can configure the EF Data Provider as follows:
Audit.Core.Configuration.Setup()
.UseEntityFramework(e => e
.AuditTypeMapper(_ => typeof(AuditLogTable))
.AuditEntityAction<AuditLogTable>((ev, entry, auditEntity) =>
{
auditEntity.User = ev.Environment.UserName;
auditEntity.Date = ev.StartDate;
auditEntity.Action = entry.Action;
auditEntity.Table = entry.Table;
auditEntity.Columns = MapColumns(entry);
})
.IgnoreMatchedProperties(true));
//..
public static List<AuditLogColumn> MapColumns(EventEntry entry)
{
if (entry.Changes != null)
{
return entry.Changes
.Select(c => new AuditLogColumn()
{
Column = c.ColumnName,
OldValue = c.OriginalValue?.ToString(),
NewValue = c.NewValue?.ToString()
}).ToList();
}
else
{
return entry.ColumnValues
.Select(c => new AuditLogColumn()
{
Column = c.Key,
NewValue = c.Value?.ToString()
}).ToList();
}
}
Hope it helps
from audit.net.
It helped, and it helped a lot
Yes, that's what I meant. Your code works very well. Thank you very much.
Have you looked at the error I get when editing a record in the database? My sample attached in previous message.
from audit.net.
Not sure why it's throwing that exception, but note you are wrongly configuring your DB context for auditing.
If you inherit your DB context from AuditDbContext
or AuditIdentityDbContext
, then you should NOT override the SaveChanges
nor use the DbContextHelper
.
You are mixing 1. and 2. from the high-level save changes interception
from audit.net.
Closing this for inactivity. Assuming the issue was solved. Of you're still having this issue please comment below
from audit.net.
Related Issues (20)
- 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
- Question for WebApi: Audit only on statuscodes HOT 2
- How to use WebApi / Serilog with .Net 4.7.2 HOT 9
- Extra Double Quote in PostgreSqlDataProvider Generated SQL HOT 2
- Sometimes when auditing to CosmosDB a CosmosException occurs HOT 5
- Since the new version of mongo-csharp-driver The Jobject for AuditTarget cannot be serialized/deserialized HOT 2
- AuditCommandInterceptor IncludeReaderResults = true results in "DataReader Open" error HOT 9
- Missing Method Exception: Microsoft.EntityFrameworkCore.Metadata.IEntityType.GetProperties() HOT 3
- Best way to "Include" related entity properties HOT 3
- Audit.EntityFramework.Core add support for EF8 Complex Types HOT 1
- Feature Request: New MongoDB interaction extension to audit a MongoDB .NET Driver instance HOT 1
- How to audit entities that have been changed\deleted because of on cascade behavior? HOT 13
- How to ingest data to AzureTableStorage month wise in the AuditTrail implementation using Audit.NET? HOT 2
- Not available to use dbContext in .UseDbContext<MyDbContext> in Audit.Core.Configuration.Setup HOT 6
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.