Audit.NET 审计 2 个不同的上下文到 2 个不同的数据库
Audit.NET auditing 2 different contexts to 2 different databases
我有一个 ASP.NET Core
应用程序,其中包含 2 个数据库的 2 个上下文(context_1 和 context_2)。我想审计两个上下文,使用 Audit.Net
来分隔数据库(context_audit_1 和 context_audit_2)所以我总共有 4 个数据库。
问题是尽管我在配置中使用了不同的上下文,它仍然在审计一个数据库。换句话说,context_1 和 context_2 正在审核 context_audit_1。
请看下面我使用的配置:
Context_1
var auditDbContextOptions = new DbContextOptionsBuilder<AuditDbContext1>()
.UseSqlServer(_connectiongStrings.PortalConnection1)
.Options;
//Audit Configuration
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.UseDbContext<AuditDbContext1>(auditDbContextOptions)
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.Action = entry.Action;
entity.TableName = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.UserId = _auditHelperService.GetUserId();
entity.TablePK = entry.PrimaryKey.First().Value.ToString();
})
.IgnoreMatchedProperties(true));
Context_2
var auditDbContextOptions = new DbContextOptionsBuilder<AuditDbContext2>()
.UseSqlServer( _connectiongStrings.PortalConnection2)
.Options;
//Audit Configuration
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.UseDbContext<AuditDbContext2>(auditDbContextOptions)
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.TableName = entry.EntityType.Name;
entity.Action = entry.Action;
entity.AuditDate = DateTime.Now;
entity.UserId = _auditHelperService.GetUserId();
entity.TablePK = entry.PrimaryKey.First().Value.ToString();
})
// the use of .IgnoreMatchedProperties(true) to avoid the library trying to set properties automatically by matching names between the audited entities and the type AuditLog
.IgnoreMatchedProperties(true));
问题是您不能设置多个默认数据提供者来保存事件。
因此第二次调用 Audit.Core.Configuration.Setup().UseEntityFramework()
时,它将覆盖之前配置的默认数据提供程序。
但是您可以使用 .UseDbContext()
的覆盖,它提供了一种方法来设置 Audit DbContext 在每个事件的基础上使用。
所以也许你可以有这样的东西:
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.UseDbContext(ev => ev.GetEntityFrameworkEvent().Database == "DB1"
? new AuditDbContext1(auditDbContextOptions1)
: new AuditDbContext2(auditDbContextOptions2))
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.Action = entry.Action;
entity.TableName = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.UserId = _auditHelperService.GetUserId();
entity.TablePK = entry.PrimaryKey.First().Value.ToString();
})
.IgnoreMatchedProperties(true));
这是假设您的 AuditLog
实体在两个审计上下文中是相同的。
我有一个 ASP.NET Core
应用程序,其中包含 2 个数据库的 2 个上下文(context_1 和 context_2)。我想审计两个上下文,使用 Audit.Net
来分隔数据库(context_audit_1 和 context_audit_2)所以我总共有 4 个数据库。
问题是尽管我在配置中使用了不同的上下文,它仍然在审计一个数据库。换句话说,context_1 和 context_2 正在审核 context_audit_1。
请看下面我使用的配置:
Context_1
var auditDbContextOptions = new DbContextOptionsBuilder<AuditDbContext1>()
.UseSqlServer(_connectiongStrings.PortalConnection1)
.Options;
//Audit Configuration
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.UseDbContext<AuditDbContext1>(auditDbContextOptions)
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.Action = entry.Action;
entity.TableName = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.UserId = _auditHelperService.GetUserId();
entity.TablePK = entry.PrimaryKey.First().Value.ToString();
})
.IgnoreMatchedProperties(true));
Context_2
var auditDbContextOptions = new DbContextOptionsBuilder<AuditDbContext2>()
.UseSqlServer( _connectiongStrings.PortalConnection2)
.Options;
//Audit Configuration
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.UseDbContext<AuditDbContext2>(auditDbContextOptions)
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.TableName = entry.EntityType.Name;
entity.Action = entry.Action;
entity.AuditDate = DateTime.Now;
entity.UserId = _auditHelperService.GetUserId();
entity.TablePK = entry.PrimaryKey.First().Value.ToString();
})
// the use of .IgnoreMatchedProperties(true) to avoid the library trying to set properties automatically by matching names between the audited entities and the type AuditLog
.IgnoreMatchedProperties(true));
问题是您不能设置多个默认数据提供者来保存事件。
因此第二次调用 Audit.Core.Configuration.Setup().UseEntityFramework()
时,它将覆盖之前配置的默认数据提供程序。
但是您可以使用 .UseDbContext()
的覆盖,它提供了一种方法来设置 Audit DbContext 在每个事件的基础上使用。
所以也许你可以有这样的东西:
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.UseDbContext(ev => ev.GetEntityFrameworkEvent().Database == "DB1"
? new AuditDbContext1(auditDbContextOptions1)
: new AuditDbContext2(auditDbContextOptions2))
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.Action = entry.Action;
entity.TableName = entry.EntityType.Name;
entity.AuditDate = DateTime.Now;
entity.UserId = _auditHelperService.GetUserId();
entity.TablePK = entry.PrimaryKey.First().Value.ToString();
})
.IgnoreMatchedProperties(true));
这是假设您的 AuditLog
实体在两个审计上下文中是相同的。