根据实例值有条件地审计 EntityChange

Auditing EntityChange conditionally based on instance value

有没有办法根据实例值有条件地禁用审计?

我有一个 table 个实体,其中有一个状态列(0 表示 'published' 或 1 表示 'still in editing')。 现在我想通过使用 Volo.Abp.Auditing 来监控 属性 的变化,但只有在发布它之后。

如果状态:

[Audited] // but not if Status stays at 1
public class Product : AuditedAggregateRoot<Guid>
{
   public int Status { get; set; }
   public string Name { get; set; }
   public string Description { get; set; }
}

您可以继承 EntityHistoryHelper 并覆盖 ShouldSaveEntityHistory:

[Dependency(ServiceLifetime.Transient, ReplaceServices = true)]
public class MyEntityHistoryHelper : EntityHistoryHelper
{
    public MyEntityHistoryHelper(
        IAuditingStore auditingStore,
        IOptions<AbpAuditingOptions> options,
        IClock clock,
        IJsonSerializer jsonSerializer,
        IAuditingHelper auditingHelper)
        : base(auditingStore, options, clock, jsonSerializer, auditingHelper)
    {
    }

    protected override bool ShouldSaveEntityHistory(EntityEntry entityEntry, bool defaultValue = false)
    {
        if (!base.ShouldSaveEntityHistory(entityEntry, defaultValue))
        {
            return false;
        }

        if (entityEntry.State == EntityState.Modified && entityEntry.Entity is Product)
        {
            var status = entityEntry.Property(nameof(Product.Status));
            if (status.OriginalValue.Equals(1) && status.CurrentValue.Equals(1))
            {
                // stays at 1: DisableAuditing
                return false;
            }
        }

        return true;
    }
}