Linger.EFCore.Audit
0.9.8
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Linger.EFCore.Audit --version 0.9.8
NuGet\Install-Package Linger.EFCore.Audit -Version 0.9.8
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="Linger.EFCore.Audit" Version="0.9.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Linger.EFCore.Audit" Version="0.9.8" />
<PackageReference Include="Linger.EFCore.Audit" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Linger.EFCore.Audit --version 0.9.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Linger.EFCore.Audit, 0.9.8"
#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.
#:package Linger.EFCore.Audit@0.9.8
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Linger.EFCore.Audit&version=0.9.8
#tool nuget:?package=Linger.EFCore.Audit&version=0.9.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Linger.EFCore.Audit
An Entity Framework Core audit trail library for automatically tracking data changes.
β¨ Features
- Automatic audit logging for Entity Framework Core operations
- Tracks entity creation, modification, and deletion
- Captures old and new values for changed properties
- Records user information for each change
- Supports soft delete
- Built-in JSON serialization for audit data
- Compatible with EF Core 9.0 and 8.0
π¦ Installation
From Visual Studio
- Open the
Solution Explorer. - Right-click on a project within your solution.
- Click on
Manage NuGet Packages.... - Click on the
Browsetab and search for "Linger.EFCore.Audit". - Click on the
Linger.EFCore.Auditpackage, select the appropriate version and click Install.
Package Manager Console
PM> Install-Package Linger.EFCore.Audit
.NET CLI Console
> dotnet add package Linger.EFCore.Audit
π Quick Start
Configuration
Add the audit functionality to your EF Core DbContext:
// 1. Add audit trail to your DbContext
public class AppDbContext : DbContext
{
public DbSet<AuditTrailEntry> AuditTrails { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Apply audit configurations
modelBuilder.ApplyAudit();
}
}
// 2. Register the audit interceptor
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(connectionString);
options.AddInterceptors(sp =>
new AuditEntitiesSaveChangesInterceptor(
sp.GetRequiredService<IAuditUserProvider>()
)
);
});
Example Usage
Automatic tracking of all changes to entities:
// Create a new entity
var user = new User
{
Name = "John Doe",
Email = "john.doe@example.com"
};
dbContext.Users.Add(user);
await dbContext.SaveChangesAsync(); // This will generate a "Created" audit record
// Modify entity
user.Email = "new.email@example.com";
await dbContext.SaveChangesAsync(); // This will generate a "Modified" audit record
// Delete entity
dbContext.Users.Remove(user);
await dbContext.SaveChangesAsync(); // This will generate a "Deleted" audit record
Setting Current User Information
Audit records can include user information by implementing the IAuditUserProvider interface:
// 1. Implement audit user provider
public class CurrentUserProvider : IAuditUserProvider
{
// Can get user info from your authentication system
public string? UserName => "john.doe";
public string GetUser() => UserName ?? "anonymous";
}
// 2. Register in your dependency injection container
services.AddScoped<IAuditUserProvider, CurrentUserProvider>();
// 3. Use with the interceptor
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(connectionString);
options.AddInterceptors(sp =>
new AuditEntitiesSaveChangesInterceptor(
sp.GetRequiredService<IAuditUserProvider>()
)
);
});
// Now all operations will automatically include user information
var product = new Product { Name = "Sample Product", Price = 100.00m };
dbContext.Products.Add(product);
await dbContext.SaveChangesAsync(); // Audit record includes user ID and username
Querying Audit Records
Audit records are stored in the AuditTrails DbSet and can be queried in various ways:
// Find all audit records related to a specific entity
var entityAudits = await dbContext.AuditTrails
.Where(a => a.EntityId == "123" && a.EntityName == "User")
.OrderBy(a => a.TimeStamp)
.ToListAsync();
// Display audit history
foreach (var audit in entityAudits)
{
Console.WriteLine($"Action: {audit.AuditType}, Time: {audit.TimeStamp}, User: {audit.Username}");
// Display all changed properties
if (audit.AffectedColumns != null)
{
Console.WriteLine("Changes:");
foreach (var column in audit.AffectedColumns)
{
var oldValue = audit.OldValues?[column];
var newValue = audit.NewValues?[column];
Console.WriteLine($" {column}: Old = {oldValue}, New = {newValue}");
}
}
}
// Query audit records by user
var userAudits = await dbContext.AuditTrails
.Where(a => a.Username == "john.doe")
.OrderByDescending(a => a.TimeStamp)
.Take(10)
.ToListAsync();
// Query audit records within a specific date range
var startDate = DateTimeOffset.Now.AddDays(-7);
var endDate = DateTimeOffset.Now;
var recentAudits = await dbContext.AuditTrails
.Where(a => a.TimeStamp >= startDate && a.TimeStamp <= endDate)
.OrderBy(a => a.TimeStamp)
.ToListAsync();
π Audit Trail Data
The AuditTrailEntry class is the main class that represents a single audit record:
public class AuditTrailEntry
{
public long Id { get; set; }
public string? Username { get; set; }
public AuditType AuditType { get; set; } // Added, Modified or Deleted
public string EntityName { get; set; }
public string? EntityId { get; set; }
public Dictionary<string, object>? OldValues { get; set; }
public Dictionary<string, object>? NewValues { get; set; }
public List<string>? AffectedColumns { get; set; }
public DateTimeOffset TimeStamp { get; set; }
public Dictionary<string, object>? Changes { get; set; }
public IEnumerable<PropertyEntry>? TempProperties { get; set; }
}
The AuditTrailEntry captures:
- Entity name and ID
- Type of change (Added/Modified/Deleted)
- Username performing the change
- Timestamp
- Old and new property values
- List of modified columns
π Automatic Tracking
- Creation audit: CreatorId, CreationTime
- Modification audit: LastModifierId, LastModificationTime
- Soft delete: IsDeleted, DeleterId, DeletionTime
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Linger.Audit (>= 0.9.8)
- Linger.EFCore (>= 0.9.8)
- Microsoft.EntityFrameworkCore (>= 10.0.0-rc.1.25451.107)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.0-rc.1.25451.107)
-
net8.0
- Linger.Audit (>= 0.9.8)
- Linger.EFCore (>= 0.9.8)
- Microsoft.EntityFrameworkCore (>= 8.0.19)
- Microsoft.EntityFrameworkCore.Relational (>= 8.0.19)
-
net9.0
- Linger.Audit (>= 0.9.8)
- Linger.EFCore (>= 0.9.8)
- Microsoft.EntityFrameworkCore (>= 9.0.8)
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.8)
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 |
|---|---|---|
| 2.0.0-preview.2 | 56 | 8/31/2026 |
| 2.0.0-preview.1 | 59 | 8/29/2026 |
| 1.6.4 | 103 | 8/16/2026 |
| 1.6.3 | 106 | 8/5/2026 |
| 1.6.2 | 122 | 8/2/2026 |
| 1.6.0 | 117 | 7/25/2026 |
| 1.5.5 | 109 | 7/23/2026 |
| 1.5.4-preview | 103 | 7/21/2026 |
| 1.5.3-preview | 99 | 7/20/2026 |
| 1.5.2-preview | 108 | 7/19/2026 |
| 1.5.1-preview | 101 | 7/15/2026 |
| 1.5.0-preview | 94 | 7/14/2026 |
| 1.4.4-preview | 112 | 6/16/2026 |
| 1.4.3-preview | 102 | 6/15/2026 |
| 1.4.2 | 116 | 5/20/2026 |
| 1.4.1-preview | 105 | 5/12/2026 |
| 1.4.0 | 112 | 5/6/2026 |
| 1.3.3-preview | 100 | 5/5/2026 |
| 1.3.2-preview | 105 | 4/29/2026 |
| 0.9.8 | 190 | 10/14/2025 |
Loading failed