2

I am trying to configure Audit.net and define my custom logic for saving logs. Is there a way to configure included entities within context? I tried this `

public ResidentMasterContext(DbContextOptions options) : base(options)
{
   AuditDataProvider = new DynamicDataProvider();
   Mode = AuditOptionMode.OptIn;
   IncludeEntityObjects = true;
   EntitySettings = new Dictionary<Type, EfEntitySettings>
   {
      {typeof(Apartment), new EfEntitySettings()}
   };         
}

` but OnScopeSaving is not firing. And when I change mode to OptOut it takes all entities

1 Answers1

0

I guess you are referring to the Audit.NET EntityFramework extension.

if you use OptIn you need to mark the included entities with [AuditInclude] attribute, or use the Include methods of the fluent API. You can check the documentation here.

An example using the fluent API for the EF configuration, to include only the entities User and UserDetail:

Audit.EntityFramework.Configuration.Setup()
    .ForContext<ResidentMasterContext>(config => config
        .IncludeEntityObjects())
    .UseOptIn()
        .Include<User>()
        .Include<UserDetail>();

An example of the output configuration:

Audit.Core.Configuration.Setup()
    .UseDynamicProvider(_ => _.OnInsertAndReplace(auditEvent =>
    {
        Console.WriteLine(auditEvent.ToJson());
    }));
thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • Thanks for answer. Yeah I have read the documentation but the problem is that I wanted to encapsulate all audit related logic in my context class. – Gevorg Narimanyan Aug 29 '18 at 10:33
  • If you want all your logic there, you could make the initialization calls on the static constructor of your context – thepirat000 Aug 29 '18 at 16:34