Replies: 2 comments 23 replies
-
I guess you mean the command interceptor events, right?. The EF data provider cannot handle the command interceptor changes, but only the changes from SaveChanges calls. This is because the EF data provider relies on the EF change tracker which is not affected by commands. One option could be to use a conditional data provider wrapper to use EF data provider for the savechanges audits and sql data provider for the rest. |
Beta Was this translation helpful? Give feedback.
-
For the high-level events, if you need to insert a record for each entry using the SqlDataProvider, I think a better way is to create a derived SQL data provider that inserts a different audit event for each EF entry. You could also create your own derived AuditEvent for this purpose. For example: public class SingleEntryAuditEvent : AuditEvent
{
public EventEntry Entry { get; set; }
} public class CustomSqlDataProvider : SqlDataProvider
{
public CustomSqlDataProvider(Action<ISqlServerProviderConfigurator> config) : base(config) {}
public override object InsertEvent(AuditEvent auditEvent)
{
foreach (var entry in auditEvent.GetEntityFrameworkEvent().Entries)
{
var entryEvent = CreateEventForEntry(auditEvent, entry);
base.InsertEvent(entryEvent);
}
return null;
}
public override async Task<object> InsertEventAsync(AuditEvent auditEvent, CancellationToken cancellationToken = new CancellationToken())
{
foreach (var entry in auditEvent.GetEntityFrameworkEvent().Entries)
{
var entryEvent = CreateEventForEntry(auditEvent, entry);
await base.InsertEventAsync(entryEvent, cancellationToken);
}
return null;
}
private static SingleEntryAuditEvent CreateEventForEntry(AuditEvent auditEvent, EventEntry entry)
{
var entryEvent = AuditEvent.FromJson<SingleEntryAuditEvent>(auditEvent.ToJson());
entryEvent.Entry = entry;
return entryEvent;
}
} So you can configure your data provider like this: Audit.Core.Configuration.Setup()
.Use(new CustomSqlDataProvider(config => config
.ConnectionString("...")
.TableName("EventLogs")
.IdColumnName("Id")
.CustomColumn("Action", ev => (ev as SingleEntryAuditEvent)?.Entry.Action ?? ev.EventType)
.CustomColumn("TableName", ev => (ev as SingleEntryAuditEvent)?.Entry.Table)
)); |
Beta Was this translation helpful? Give feedback.
-
Hi
I first started using the EF Provider but found out that it does not track low level changes, so I had to switch to the SqlServer provider but not sure how to set it up properly.
I had for EF
Now I come up with this for SqlServer
I had to do some if statement checks and I found for the low level ones there is no Table name or action (Though EventType).
Is this the way I have to do it, or is there a better way of doing it?
Beta Was this translation helpful? Give feedback.
All reactions