EF.AuditLog 1.0.0

dotnet add package EF.AuditLog --version 1.0.0                
NuGet\Install-Package EF.AuditLog -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EF.AuditLog" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EF.AuditLog --version 1.0.0                
#r "nuget: EF.AuditLog, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EF.AuditLog as a Cake Addin
#addin nuget:?package=EF.AuditLog&version=1.0.0

// Install EF.AuditLog as a Cake Tool
#tool nuget:?package=EF.AuditLog&version=1.0.0                

AuditLog

A plugin for Microsoft.EntityFrameworkCore that enables automatic recording of data change history.

How to use

To enable automatic recording of data changes in your application, configure your DbContext to utilize the audit logging functionality.

Follow these steps:

  1. Configure Audit Settings
    In your DbContext class, configure the audit settings by calling the ApplyAuditSettings method. This allows you to define whether all entities should be audited by default or only those explicitly configured.

  2. Apply Audit Entry Configuration
    Specify how audit entries should be stored by calling the ApplyAuditEntryConfiguration method. You can pass a custom audit entity type, such as CustomAuditEntity, to define the structure of your audit logs.

  3. Load Audit Configurations If you have custom audit configurations defined in your assembly, use the ApplyAuditConfigurationsFromAssembly method. This will automatically load and apply these configurations based on the types defined in your application.

internal sealed class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
	    .ApplyAuditSettings(options => options.OnlyConfiguredAudited())
            .ApplyAuditEntryConfiguration<CustomAuditEntity>()
            .ApplyAuditConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
    }
}

Registering the Audit Save Changes Interceptor

To enable automatic audit logging for your Entity Framework Core context, you need to register an interceptor that will monitor and log changes during SaveChanges operations. This can be done by adding an interceptor to the DbContext when configuring it in your application's service container.

Here’s an example of how to register the AuditSaveChangesInterceptor:

builder.Services.AddDbContext<AppDbContext>(options =>
{
	options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"));
	options.AddInterceptors(new AuditSaveChangesInterceptor());
});

Using a Custom Interceptor with a Custom Entity

If your application requires a custom audit entity, you can extend the functionality by creating a custom interceptor.
This custom interceptor can override the behavior of the default interceptor to accommodate the specific fields or properties your audit entity might have.

For example, consider a custom audit entity CustomAuditEntity that includes an additional CreatedBy property:

public class CustomAuditEntity : AuditEntry
{
    /// <summary>
    /// Gets or sets the entity identifier.
    /// </summary>
    public string CreatedBy { get; set; }
}

To support this custom entity, you would create a custom interceptor by extending AuditSaveChangesInterceptorBase:

internal sealed class AuditSaveChangesInterceptor : AuditSaveChangesInterceptorBase<CustomAuditEntity>
{
    protected override Func<CustomAuditEntity> AuditEntryFactory()
    {
        return () => new CustomAuditEntity()
        {
            CreatedBy = "some logic to get the current user",
        };
    }
}

Ignoring Specific Properties in Audit Logs

The UserAuditConfiguration class is used to define how the User entity is handled during auditing. By implementing the IAuditConfiguration<User> interface, you can control which properties are included or excluded in the audit records.

internal sealed class UserAuditConfiguration : IAuditConfiguration<User>
{
    public void Configure(AuditTypeConfigurationBuilder<User> configurationBuilder)
    {
        configurationBuilder.IgnoreProperty(x => x.Password);
    }
}

Excluding an Entire Entity from Audit Logs

Alternatively, the UserAuditConfiguration class can be configured to completely exclude the User entity from being audited. This means that any changes to User records will not be logged.

internal sealed class UserAuditConfiguration : IAuditConfiguration<User>
{
    public void Configure(AuditTypeConfigurationBuilder<User> configurationBuilder)
    {
        configurationBuilder.ExcludeEntity();
    }
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 537 8/2/2024