Levge.AuditLog 2.3.0

dotnet add package Levge.AuditLog --version 2.3.0
                    
NuGet\Install-Package Levge.AuditLog -Version 2.3.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="Levge.AuditLog" Version="2.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Levge.AuditLog" Version="2.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Levge.AuditLog" />
                    
Project file
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 Levge.AuditLog --version 2.3.0
                    
#r "nuget: Levge.AuditLog, 2.3.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.
#:package Levge.AuditLog@2.3.0
                    
#: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=Levge.AuditLog&version=2.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Levge.AuditLog&version=2.3.0
                    
Install as a Cake Tool

Publish NuGet Package

Levge.AuditLog

EF Core ISaveChangesInterceptor tabanlı, yüksek performanslı ve esnek otomatik denetim kaydı (audit log) altyapısı.
.NET 10+ ve EF Core 10+ için optimize edilmiştir.

Özellikler

  • Kalıtımsız Entegrasyon: IdentityDbContext gibi mevcut hiyerarşileri bozmadan IAuditableDbContext ile entegre olur.
  • Otomatik Metadata: IAuditable ve ISoftDeletable varlıklarını otomatik yakalar.
  • Soft Delete Desteği: Delete işlemlerini otomatik olarak IsDeleted güncellemesine çevirir ve audit log'a "Delete" olarak işler.
  • Performans: ExecuteDeleteAsync ile toplu temizleme ve optimize edilmiş indexler.
  • Esneklik: Belirli entity'leri audit dışı bırakma ve dinamik olarak audit log yazımını durdurma.
  • Bileşik PK Desteği: Bileşik birincil anahtarlar JSON olarak saklanır.

İçindekiler


Kurulum

dotnet add package Levge.AuditLog

Not: Bu paket Levge.Identity ve Levge.Domain paketlerine bağımlıdır. Projenizde bu paketlerin de kurulu olduğundan emin olun.


Hızlı Başlangıç (Seçenek A) — LevgeAuditDbContext ile

Eğer yeni bir projeye başlıyorsanız veya mevcut bir base DbContext'iniz yoksa:

using Levge.AuditLog.DbContexts;
using Microsoft.EntityFrameworkCore;

public class AppDbContext : LevgeAuditDbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

    public DbSet<Order> Orders { get; set; } = null!;

    // Belirli entity'leri audit dışı bırakmak için:
    public override HashSet<Type> ExcludedAuditEntities { get; } = [typeof(InternalLog)];
}

Gelişmiş Entegrasyon (Seçenek B) — IAuditableDbContext ile

IdentityDbContext veya MultiTenantDbContext gibi başka bir sınıftan türetilen mevcut yapılar için:

using Levge.AuditLog.Entities;
using Levge.AuditLog.Interfaces;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public class AppDbContext : IdentityDbContext<AppUser>, IAuditableDbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

    // IAuditableDbContext Implementasyonu
    public DbSet<AuditLogEntry> AuditLogs { get; set; } = null!;
    public bool UseAuditLogs { get; set; } = true;
    public HashSet<Type> ExcludedAuditEntities { get; } = [];

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        // Performans indexlerini ve tablo yapılandırmasını ekle
        modelBuilder.ConfigureLevgeAuditLog();
    }
}

Yapılandırma (Program.cs)

var builder = WebApplication.CreateBuilder(args);

// 1. AuditLog servislerini kaydet
builder.Services.AddLevgeAuditLog<AppDbContext>(opt =>
{
    opt.EnableCleanupJob = true; // Eski kayıtları otomatik sil
    opt.RetentionDays    = 90;   // 90 günlük kayıt tut
    opt.CleanupTime      = new TimeOnly(3, 0); // UTC 03:00'da çalıştır
});

// 2. DbContext'e Interceptor'ı bağla
builder.Services.AddDbContext<AppDbContext>((sp, opt) =>
    opt.UseNpgsql(builder.Configuration.GetConnectionString("Default"))
       .AddLevgeAuditInterceptors(sp)); // ← Kritik: Interceptor kaydı

// 3. ICurrentUser için Identity yapılandırması gereklidir
builder.Services.AddLevgeIdentity(jwtOptions); 

Varlık Tanımlama

Audit log tutulmasını istediğiniz varlıkların IAuditable interface'ini implemente etmesi yeterlidir.

public class Product : IAuditable, ISoftDeletable
{
    public long Id { get; set; }
    public string Name { get; set; } = null!;

    // Otomatik yönetilen alanlar
    public DateTime CreatedAt { get; set; }
    public long CreatedByUserId { get; set; }
    public DateTime? UpdatedAt { get; set; }
    public long? UpdatedByUserId { get; set; }
    
    public bool IsDeleted { get; set; }
    public DateTime? DeletedAt { get; set; }
    public long? DeletedByUserId { get; set; }
}

AuditLogEntry Şeması

Kolon Tür Açıklama
Id long Birincil anahtar
ChangedByUserId long? Değişikliği yapan kullanıcı (System=0)
Type string Create, Update, Delete
TableName string Etkilenen tablo adı
ChangedAt DateTime UTC değişiklik zamanı
EntityId string? Tekil PK değeri (Hızlı sorgulama için)
KeyValues string? Tüm PK değerleri (JSON)
OldValues string? Eski değerler (JSON)
NewValues string? Yeni değerler (JSON)
AffectedColumns string? Değişen kolon adları

IAuditLogService Kullanımı

public class OrderService(IAuditLogService auditLogService)
{
    public async Task<List<AuditLogEntry>> GetOrderHistory(long orderId)
    {
        // "Orders" tablosundaki belirli bir kaydın geçmişini getirir
        return await auditLogService.GetLogsByObjectAsync("Orders", orderId);
    }
}

Arka Plan Temizleme Jobu

EnableCleanupJob = true olarak yapılandırıldığında, sistem her gün belirlenen saatte çalışarak RetentionDays süresinden eski kayıtları kalıcı olarak siler. Bu işlem ExecuteDeleteAsync kullandığı için milyonlarca kayıtta dahi veritabanını yormaz.


Önemli Notlar ve İpuçları

  • Bileşik Anahtarlar: Eğer bir entity bileşik anahtara (Composite Key) sahipse, EntityId alanı null olur ve tüm anahtar değerleri KeyValues kolonunda JSON olarak saklanır.
  • Tenant Desteği: Paket, çok kiracılı (multi-tenant) yapılarda kiracı filtrelerine uyumludur. Ancak AuditLogEntry tablosunun kendisinde TenantId bulunmaz; eğer logları kiracı bazlı izole etmek isterseniz, IAuditableDbContext.AuditLogs üzerinde Global Query Filter tanımlayabilirsiniz.
  • Entity Exclusion: Hassas veriler içeren tabloları (ör. PasswordHashes) audit dışında bırakmak için ExcludedAuditEntities listesini kullanın.
  • Performans: ConfigureLevgeAuditLog metodu; TableName, EntityId, ChangedByUserId ve ChangedAt alanlarına otomatik index ekler.
  • ICurrentUser: Levge.Identity üzerinden gelen ICurrentUser.UserId değeri null ise (ör. bir background job işlemi), CreatedByUserId değeri varsayılan olarak 0 atanır.
Product Compatible and additional computed target framework versions.
.NET 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.

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.3.0 101 4/29/2026
2.1.0 104 4/27/2026
2.0.1 96 4/27/2026
1.1.50 166 1/22/2026
1.1.48 534 11/10/2025
1.1.47 328 7/3/2025
1.1.46 292 7/3/2025
1.1.45 283 7/3/2025
1.1.44 293 7/3/2025
1.1.43 290 7/3/2025
1.1.42 299 7/2/2025
1.1.41 295 7/2/2025
1.0.20 273 6/21/2025
1.0.19 270 6/21/2025
1.0.18 253 6/21/2025
1.0.17 256 6/21/2025
1.0.16 313 6/19/2025
1.0.15 321 6/19/2025
1.0.13 306 6/19/2025
1.0.12 310 6/19/2025
Loading failed