Linger.Audit
0.9.5
dotnet add package Linger.Audit --version 0.9.5
NuGet\Install-Package Linger.Audit -Version 0.9.5
<PackageReference Include="Linger.Audit" Version="0.9.5" />
<PackageVersion Include="Linger.Audit" Version="0.9.5" />
<PackageReference Include="Linger.Audit" />
paket add Linger.Audit --version 0.9.5
#r "nuget: Linger.Audit, 0.9.5"
#:package Linger.Audit@0.9.5
#addin nuget:?package=Linger.Audit&version=0.9.5
#tool nuget:?package=Linger.Audit&version=0.9.5
Linger.Audit
A lightweight .NET auditing library that provides base classes and interfaces for entity auditing.
π Table of Contents
- β¨ Features
- π¦ Installation
- π Quick Start
- π‘ Usage Examples
- π§ Advanced Configuration
- π§© Class Diagram Overview
- π Interface and Base Class Reference
- π License
β¨ Features
- Multi-target framework support (.NET 9.0/.NET 8.0/.NET 6.0/NetStandard 2.0)
- Full audit trail tracking (creation, modification, deletion)
- Generic entity support with type-safe IDs
- Soft delete capability
- Built-in audit timestamps and user tracking
- Nullable reference types enabled
- MIT licensed
π¦ Installation
From Visual Studio
- Open the
Solution Explorer
- Right-click on your project
- Select
Manage NuGet Packages...
- Click the
Browse
tab and search for "Linger.Audit" - Click
Install
Package Manager Console
Install-Package Linger.Audit
.NET CLI
dotnet add package Linger.Audit
π Quick Start
Basic Entities
Inherit from base entity classes to get ID properties:
// Simple entity with Guid ID type
public class Product : BaseEntity<Guid>
{
public string Name { get; set; } = null!;
public decimal Price { get; set; }
public string Description { get; set; } = null!;
}
// Entity with int ID type
public class Category : BaseEntity<int>
{
public string Name { get; set; } = null!;
}
// Entity with string ID type
public class Tag : BaseEntity<string>
{
public string Value { get; set; } = null!;
}
Creation Audit Entities
Track creation time and creator:
// Record when and who created a comment
public class Comment : CreationAuditEntity<Guid>
{
public string Text { get; set; } = null!;
public Guid ProductId { get; set; }
// Inherited properties:
// public string? CreatorId { get; set; }
// public DateTimeOffset CreationTime { get; set; }
}
Full Audit Entities
Track creation, modification, and deletion information:
// User entity with full audit tracking
public class User : FullAuditEntity<Guid>
{
public string Username { get; set; } = null!;
public string Email { get; set; } = null!;
// Inherited properties:
// Creation
// public string? CreatorId { get; set; }
// public DateTimeOffset CreationTime { get; set; }
// Modification
// public string? LastModifierId { get; set; }
// public DateTimeOffset? LastModificationTime { get; set; }
// Deletion
// public bool IsDeleted { get; set; }
// public string? DeleterId { get; set; }
// public DateTimeOffset? DeletionTime { get; set; }
}
π‘ Usage Examples
Setting Up Current User Context
Use audit entities in your application services, and the system will automatically populate audit fields:
// In your application service
public class ProductService : IProductService
{
private readonly IRepository<Product, Guid> _productRepository;
private readonly IAuditUserProvider _auditUserProvider;
public ProductService(IRepository<Product, Guid> productRepository, IAuditUserProvider auditUserProvider)
{
_productRepository = productRepository;
_auditUserProvider = auditUserProvider;
}
public async Task<Product> CreateProductAsync(string name, decimal price)
{
var product = new Product
{
Name = name,
Price = price,
// ID, CreatorId and CreationTime will be set automatically when saving
};
await _productRepository.AddAsync(product);
await _productRepository.SaveChangesAsync();
return product;
}
}
EF Core Integration
Configure EF Core DbContext to automatically handle audit fields:
// Example of handling audit fields in EF Core
public class AppDbContext : DbContext
{
private readonly IAuditUserProvider _auditUserProvider;
public AppDbContext(DbContextOptions options, IAuditUserProvider auditUserProvider)
: base(options)
{
_auditUserProvider = auditUserProvider;
}
public DbSet<Product> Products { get; set; } = null!;
public DbSet<User> Users { get; set; } = null!;
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
UpdateAuditFields();
return base.SaveChangesAsync(cancellationToken);
}
private void UpdateAuditFields()
{
var userId = _auditUserProvider.GetUser();
var now = DateTimeOffset.UtcNow;
foreach (var entry in ChangeTracker.Entries<IEntity>())
{
if (entry.State == EntityState.Added)
{
if (entry.Entity is ICreationAuditEntity creationAuditEntity)
{
creationAuditEntity.CreationTime = now;
creationAuditEntity.CreatorId = userId;
}
}
else if (entry.State == EntityState.Modified)
{
if (entry.Entity is IModificationAuditEntity modificationAuditEntity)
{
modificationAuditEntity.LastModificationTime = now;
modificationAuditEntity.LastModifierId = userId;
}
}
else if (entry.State == EntityState.Deleted && entry.Entity is ISoftDelete softDeleteEntity)
{
// Convert to soft delete
entry.State = EntityState.Modified;
softDeleteEntity.IsDeleted = true;
if (entry.Entity is IDeletionAuditEntity deletionAuditEntity)
{
deletionAuditEntity.DeletionTime = now;
deletionAuditEntity.DeleterId = userId;
}
}
}
}
}
Soft Delete Filtering
Use global query filters to automatically filter soft-deleted entities:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Apply soft delete filter for all entities implementing ISoftDelete
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (typeof(ISoftDelete).IsAssignableFrom(entityType.ClrType))
{
var parameter = Expression.Parameter(entityType.ClrType, "e");
var property = Expression.PropertyOrField(parameter, nameof(ISoftDelete.IsDeleted));
var condition = Expression.Not(property);
var lambda = Expression.Lambda(condition, parameter);
modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda);
}
}
}
π§ Advanced Configuration
Handling Legacy Database DateTime Types
In real-world projects, you may need to integrate with existing databases that use datetime
types instead of datetimeoffset
. When you cannot modify the database table structure, you need to configure data type conversion in EF Core.
Use Cases:
- Database tables already exist using
datetime
type - Cannot modify existing table structure
- Need to use
DateTimeOffset
type for auditing in your application
Solution:
public class UserEntityConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> entity)
{
// Configure CreationTime field
entity.Property(e => e.CreationTime)
.HasColumnType("datetime")
.HasConversion(
// When saving to database: DateTimeOffset -> DateTime
v => v.ToDateTime(),
// When reading from database: DateTime -> DateTimeOffset
v => new DateTimeOffset(v)
);
// Configure LastModificationTime field (nullable type)
entity.Property(e => e.LastModificationTime)
.HasColumnType("datetime")
.HasConversion(
// When saving to database: DateTimeOffset? -> DateTime?
v => v.HasValue ? v.Value.ToDateTime() : (DateTime?)null,
// When reading from database: DateTime? -> DateTimeOffset?
v => v.HasValue ? new DateTimeOffset(v.Value, TimeSpan.Zero) : (DateTimeOffset?)null
);
// Configure DeletionTime field (if using FullAuditEntity)
entity.Property(e => e.DeletionTime)
.HasColumnType("datetime")
.HasConversion(
v => v.HasValue ? v.Value.ToDateTime() : (DateTime?)null,
v => v.HasValue ? new DateTimeOffset(v.Value, TimeSpan.Zero) : (DateTimeOffset?)null
);
// Configure audit user fields
entity.Property(e => e.CreatorId)
.HasMaxLength(30)
.IsUnicode(false);
entity.Property(e => e.LastModifierId)
.HasMaxLength(30)
.IsUnicode(false);
entity.Property(e => e.DeleterId)
.HasMaxLength(30)
.IsUnicode(false);
OnConfigurePartial(entity);
}
partial void OnConfigurePartial(EntityTypeBuilder<User> entity);
}
Important Notes:
- Timezone information will be lost during conversion, recommend using UTC time consistently in your application
TimeSpan.Zero
represents UTC timezone offset- Ensure all times stored in the database are in UTC to avoid timezone confusion
οΏ½ Class Diagram Overview
Relationships between main classes and interfaces:
BaseEntity<T>
|
ββ CreationAuditEntity<T>
| |
| ββ AuditEntity<T>
| | |
| | ββ FullAuditEntity<T>
| |
| ββ [Custom Entity]
|
ββ [Custom Entity]
π Interface and Base Class Reference
IEntity<T> Interface
Defines an entity with typed ID:
public interface IEntity<T> : IEntity
{
T Id { get; set; }
}
ISoftDelete Interface
Enables soft delete functionality:
public interface ISoftDelete
{
bool IsDeleted { get; set; }
}
BaseEntity Class
Base class for entities:
public abstract class BaseEntity<T> : IEntity<T>
{
public T Id { get; set; } = default!;
}
CreationAuditEntity Class
Base class that tracks creation information:
public abstract class CreationAuditEntity : ICreationAuditEntity
{
public string? CreatorId { get; set; }
public DateTimeOffset CreationTime { get; set; }
}
AuditEntity Class
Base class that tracks creation and modification information:
public abstract class AuditEntity : CreationAuditEntity, IModificationAuditEntity
{
public string? LastModifierId { get; set; }
public DateTimeOffset? LastModificationTime { get; set; }
}
FullAuditEntity Class
Base class that tracks creation, modification, and deletion information:
public abstract class FullAuditEntity : AuditEntity, IDeletionAuditEntity, ISoftDelete
{
public bool IsDeleted { get; set; }
public string? DeleterId { get; set; }
public DateTimeOffset? DeletionTime { get; set; }
}
οΏ½ License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Linger.Audit:
Package | Downloads |
---|---|
Linger.EFCore.Audit
An Entity Framework Core audit trail library for automatically tracking data changes. Captures entity creation, modification, and deletion events with old and new values. Provides configurable audit logging with support for user tracking and timestamping. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
0.9.5 | 136 | 9/28/2025 |
0.9.4-preview | 159 | 9/25/2025 |
0.9.3-preview | 172 | 9/22/2025 |
0.9.1-preview | 289 | 9/16/2025 |
0.9.0-preview | 112 | 9/12/2025 |
0.8.5-preview | 178 | 8/31/2025 |
0.8.4-preview | 301 | 8/25/2025 |
0.8.3-preview | 163 | 8/20/2025 |
0.8.2-preview | 198 | 8/4/2025 |
0.8.1-preview | 125 | 7/30/2025 |
0.8.0-preview | 565 | 7/22/2025 |
0.7.2 | 182 | 6/3/2025 |
0.7.1 | 189 | 5/21/2025 |
0.7.0 | 186 | 5/19/2025 |
0.6.0-alpha | 194 | 4/28/2025 |
0.5.0-alpha | 190 | 4/10/2025 |
0.4.0-alpha | 185 | 4/1/2025 |
0.3.3-alpha | 194 | 3/19/2025 |
0.3.2-alpha | 189 | 3/17/2025 |
0.3.1-alpha | 175 | 3/16/2025 |
0.3.0-alpha | 243 | 3/6/2025 |
0.2.0-alpha | 131 | 2/9/2025 |
0.1.2-alpha | 125 | 12/17/2024 |
0.1.1-alpha | 104 | 12/17/2024 |
0.1.0-alpha | 120 | 12/6/2024 |
0.0.3-alpha | 121 | 11/27/2024 |
0.0.2-alpha | 115 | 10/3/2024 |
0.0.1-alpha | 132 | 9/28/2024 |