AuditLog.Generator
0.4.3
See the version list below for details.
dotnet add package AuditLog.Generator --version 0.4.3
NuGet\Install-Package AuditLog.Generator -Version 0.4.3
<PackageReference Include="AuditLog.Generator" Version="0.4.3"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="AuditLog.Generator" Version="0.4.3" />
<PackageReference Include="AuditLog.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add AuditLog.Generator --version 0.4.3
#r "nuget: AuditLog.Generator, 0.4.3"
#:package AuditLog.Generator@0.4.3
#addin nuget:?package=AuditLog.Generator&version=0.4.3
#tool nuget:?package=AuditLog.Generator&version=0.4.3
AuditLog
Biblioteca de auditoria automática para EF Core com Source Generators Roslyn.
Pacotes
| Pacote | Descrição |
|---|---|
AuditLog.Abstractions |
Contratos: AuditConfigurator<T>, IAuditDescriptor, builders |
AuditLog.EntityFrameworkCore |
Integração EF Core: AuditSaveInterceptor, extensions |
AuditLog.Generator |
Source generator — gera *AuditLog, maps, descriptors |
AuditLog.EntityFrameworkCore.SoftDelete |
Runtime: interfaces, interceptor, query filters para soft delete |
AuditLog.Generator.SoftDelete |
Source generator — gera handlers tipados de cascade/restrict/set-null |
AuditLog — Auditoria de Entidades
1. Defina um configurador
[GenerateAuditLog]
public sealed class PacienteAuditConfigurator : AuditConfigurator<Paciente>
{
public PacienteAuditConfigurator()
{
For(x => x.Id).Key();
For(x => x.Nome).HasMaxLength(200).IsRequired();
For(x => x.Cpf).Sensitive().HasMaxLength(11);
For(x => x.DataAtualizacao).Ignore();
}
}
2. Adicione o interceptor no DbContext
public class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.AddInterceptors(new AuditSaveInterceptor());
}
}
3. O generator produz automaticamente
PacienteAuditLog— tabela de auditoria com snapshot dos dadosPacienteAuditLogDescriptor— mapeiaPaciente → PacienteAuditLogPacienteAuditLogEntityMap— EF Core configuration (column types, max length)ServiceCollectionExtensions.AddGeneratedAuditLogs()— DI registration
SoftDelete — Exclusão Lógica
Dois pacotes complementares:
| Package | Função |
|---|---|
AuditLog.EntityFrameworkCore.SoftDelete |
Runtime: interceptor, interfaces, query filters |
AuditLog.Generator.SoftDelete |
Source generator (opcional): gera handlers com tipagem forte |
Instalação
<ItemGroup>
<PackageReference Include="AuditLog.EntityFrameworkCore.SoftDelete" Version="1.0.0" />
<PackageReference Include="AuditLog.Generator.SoftDelete" Version="1.0.0" />
</ItemGroup>
1. Implemente ISoftDeleteEntity nas entidades
public class Paciente : ISoftDeleteEntity
{
public Guid Id { get; set; }
public string Nome { get; set; }
// Obrigatório para soft delete
public bool IsDeleted { get; set; }
public DateTime? DeletedAt { get; set; }
// Relacionamentos (Fluent API configurada no DbContext)
public List<Notificacao> Notificacoes { get; set; } = [];
}
2. Marque o DbContext com [GenerateSoftDelete]
using AuditLog.EntityFrameworkCore.SoftDelete;
[GenerateSoftDelete]
public class AppDbContext : DbContext
{
public DbSet<Paciente> Pacientes { get; set; }
public DbSet<Notificacao> Notificacoes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Paciente>(e =>
{
e.HasMany(x => x.Notificacoes)
.WithOne(x => x.Paciente)
.HasForeignKey(x => x.PacienteId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.ApplySoftDeleteQueryFilter();
}
}
3. Registre na DI
// Com o source generator (handlers tipados)
var registry = new SoftDeleteHandlerRegistry();
registry.AddGeneratedSoftDeleteHandlers();
services.AddSoftDelete(registry);
// Ou sem o generator (fallback via reflection)
services.AddSoftDelete();
4. Use normalmente
db.Pacientes.Remove(paciente);
await db.SaveChangesAsync();
// → IsDeleted = true, DeletedAt = now
// → Cascade: Notificacoes também são marcadas como deletadas
// → Query filter global: db.Pacientes retorna apenas não-deletados
Comportamentos por FK
OnDelete() |
Efeito |
|---|---|
Cascade |
Dependentes são soft-deletados recursivamente |
Restrict |
Lança RestrictDeleteViolationException se houver dependentes |
SetNull |
FK dos dependentes é setada como null |
Convenções (quando OnDelete não é especificado)
| Navigation | Comportamento |
|---|---|
Collection (List<T>) |
Cascade |
Reference (T) |
Restrict |
FK nullable (Guid?) |
SetNull |
Consultas
// Query filter automático — só não-deletados
db.Pacientes.ToList();
// Incluir deletados
db.Pacientes.IgnoreQueryFilters().ToList();
Suporte a herança indireta de IEntityTypeConfiguration<T>
O gerador detecta entity maps que implementam IEntityTypeConfiguration<T> através de toda a cadeia de herança, incluindo casos como AuditEntityMap<T> → IContextEntityMap<T> → IEntityTypeConfiguration<T>. Isso funciona tanto com ApplyConfiguration(new ConcreteEntityMap()) quanto com ApplyConfigurationsFromAssembly().
Descoberta de entidades sem DbSet<T>
O gerador descobre entidades mesmo quando o DbContext não possui propriedades DbSet<T>, desde que as entidades sejam registradas via modelBuilder.Entity<T>() no OnModelCreating ou via ApplyConfiguration/ApplyConfigurationsFromAssembly com entity maps que implementam IEntityTypeConfiguration<T>.
| 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 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. net9.0 was computed. 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 was computed. 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.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.