question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Where to put Audit.Core.Configuration.Setup()

See original GitHub issue

Where can/should this get called? I could not find in the readme the location to call Audit.Core.Configuration.Setup().

I first tried calling Audit.Core.Configuration.Setup() in the database context OnModelCreating(), and the audit table was created. However, there were some null errors when trying to save to the audit table.

Second, I tried tried calling Audit.Core.Configuration.Setup() in the database context OnConfiguring(), since I saw it in another issue. That resulted in the audit table being deleted, so that doesn’t seem like the right place.

Your configuration call to Audit.Core.Configuration.Setup() should be done only once, at the startup of your application.

_Originally posted by @thepirat000 in https://github.com/thepirat000/Audit.NET/issues/247#issuecomment-539686730_

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mrlifecommented, Mar 16, 2021

Thank you, I fixed by adding an explicit key property to the Audit_* model classes so that the Id matched up with the audited model and EF Core uses the explicit key property for its tracking purposes.

e.g.

public class MyModel
{
    public int Id { get; set; }
    public string Property { get; set; }
}
 
public class Audit_MyModel : IAudit
{
    [Key]
    public int AuditId { get; set; }
    public int Id { get; set; }
    public string Property { get; set; }
 
    public string AuditAction { get; set; }
    public DateTime AuditDate { get; set; }
}

I think your library is wonderful and appreciate your work to create and maintain it.

1reaction
thepirat000commented, Mar 16, 2021

I don’t know what the users of the library usually do. There are many data providers for the audit output saving, and there are many use cases for the EntityFramework data provider.

For the DI part, you should be able to access any of your registered services, or the IServiceProvider as a parameter on your Startup.Configure() method and then use it on the setup delegate, for example:

public void Configure(IApplicationBuilder app, IServiceProvider svcProvider)
{
    Audit.Core.Configuration.Setup()
        .UseEntityFramework(ef => ef
            .AuditTypeExplicitMapper(m => m
                .Map<Order, Audit_Order>()
                .Map<OrderItem, Audit_OrderItem>()
                .AuditEntityAction<IAudit>((evt, entry, auditEntity) =>
                {
                    var svc = svcProvider.GetRequiredService<IYourService>();
                    // ...
                })
            )
        );
    // ...
}

or if it’s a singleton, you could just:

public void Configure(IApplicationBuilder app, IYourService svc)
{
    Audit.Core.Configuration.Setup()
        .UseEntityFramework(ef => ef
            .AuditTypeExplicitMapper(m => m
                .Map<Order, Audit_Order>()
                .Map<OrderItem, Audit_OrderItem>()
                .AuditEntityAction<IAudit>((evt, entry, auditEntity) =>
                {
                    // use svc here
                })
            )
        );
    // ...
}

Is the DbSet you need to access on the same DbContext that is being audited? If so, you can access it with GetDbContext() method on the EF event:

using Audit.EntityFramework;
//...
Audit.Core.Configuration.Setup()
      .UseEntityFramework(ef => ef
          .AuditTypeExplicitMapper(m => m
              .Map<Order, Audit_Order>()
              .Map<OrderItem, Audit_OrderItem>()
              .AuditEntityAction<IAudit>((evt, entry, auditEntity) =>
              {
                  var svc = svcProvider.GetRequiredService<IYourService>();
                  var dbContext = evt.GetEntityFrameworkEvent().GetDbContext();
                  // ...
              })
          )
      );
Read more comments on GitHub >

github_iconTop Results From Across the Web

Audit.Net doesn't work with default asp.net Core DI
Audit.Core.Configuration.Setup() .UseFactory(() => services.GetRequiredService<AuditDataProvider>()). Note the call to auditScope.
Read more >
Audit.EntityFramework
An extensible framework to audit executing operations in .NET and .NET Core. - Audit.NET/src/Audit.EntityFramework/README.md at master · thepirat000/Audit.
Read more >
Audit.NET
To install the package run the following command on the Package Manager Console: ... Defaults to the DataProvider configured on Audit.Core.Configuration.
Read more >
How to set up auditing in Entity Framework Core
Learn how to set up simple entity auditing and track changes in Entity Framework ... NET Core API project - check out the...
Read more >
Audit with Entity Framework Core
To install the template and create a new project from the command line, ... Audit.Core.Configuration.Setup() .UseFileLogProvider(_ => _.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found