为日志记录程序添加数据库审计日志功能
代码地址:https://github.com/fallTakeMan/Prime
使用示例
builder.Services.AddDbContext<ApiDbContext>(options => { options.UseNpgsql(builder.Configuration.GetConnectionString("PrimeApi")); options.AddPrimeAuditingInterceptor(builder.Services); });
数据库审计日志功能参考 efcore 文档。扩展了 AddInterceptors 方法,需要将 builder.Services 传给侦听器,并且在侦听器中实现获取数据库操作用户信息。
public static DbContextOptionsBuilder AddPrimeAuditingInterceptor(this DbContextOptionsBuilder builder, IServiceCollection services) { var serviceProvider = services.BuildServiceProvider(); var prime = serviceProvider.GetService<PrimeOptions>(); if(prime is null) { throw new ArgumentNullException(nameof(PrimeOptions)); } builder.AddInterceptors(new PrimeAuditingInterceptor(serviceProvider)); return builder; }


浙公网安备 33010602011771号