entity-frameworkasp.net-coreaudit.net

How do I use Audit.NET Entity Framework Data Provider to save Audit.NET WebAPI audit logs?


I am having difficulty understanding the documentation for the Audit.NET Entity Framework Data Provider, to save Audit.NET WebAPI audit logs to my database.

This is how I have my Audit configuration set, just to test. I have a breakpoint inside the AuditEntityAction on entity.ChangeType = ev.EventType, but this never gets hit when I call an audited action on my controller.

Audit.Core.Configuration.Setup()
    .UseEntityFramework(x => 
        x.AuditTypeMapper(t => typeof(AuditLog))
            .AuditEntityAction<AuditLog>((ev, entry, entity) =>
            {
                entity.ChangeType = ev.EventType;
                entity.ObjectType = entry.EntityType.Name;
                entity.PrimaryKey = "test";
                entity.TableName = "test";
                entity.UserId = entry.CustomFields[UserIdField].ToString();
            })
            .IgnoreMatchedProperties()
        );

On my controller action, I have the decorator:

[AuditApi(EventTypeName = "Organisation:Create", IncludeRequestBody = true, IncludeResponseBody = true)]

Is this correct? I am not very clear on this, and I would appreciate some pointers.


Solution

  • The Entity Framework Data Provider is part of the library Audit.EntityFramework and was designed to exclusively store the audits that are generated by an audited Entity Framework DbContext.

    So it will not work for WebApi events of any other kind of event.

    Here you can see how the audit event is discarded if it's not an AuditEventEntityFramework

    So you should create your own Custom Data Provider or maybe use the SQL Data Provider.