Audit.NET - Entity Framework - 根据操作过滤审核事件

Audit.NET - Entity Framework - Filter audit events based on Action

我正在使用 Audit.NET 和 Entity Framework 使用 MVC 应用程序 运行 .NET 4.6.1.

实施审计

我需要能够根据正在执行的数据库操作来限制审计。我查看了文档,发现我可以通过编写自定义代码通过 AuditEntityAction 来控制它。

我正在寻找的是能够将我的数据模型归因于指示哪些操作应该被审计。如果这个逻辑不存在,我可以创建它,但我想我会先与开发社区核实一下。下面是我正在寻找的示例。

[AuditInclude("DELETE")]

[AuditInclude("DELETE", "UPDATE")]

您可以将函数 AuditEntityAction 设为 return 布尔值,以指示是在输出日志中包含 (true) 还是忽略 (false) 实体。

例如:

Audit.Core.Configuration.Setup()
    .UseEntityFramework(ef => ef
        .AuditTypeMapper(t => typeof(AuditLog))
        .AuditEntityAction<AuditLog>((ev, entry, entity) =>
        {
            // your code...

            return entry.EntityType == typeof(Employee) && entry.Action == "Delete";
        })
        .IgnoreMatchedProperties(true));

我接受了“thepirate000”的回答,因为它为我提供了解决方案,但最终我的实现方式略有不同。我创建了一个名为 AuditIncludeActionAttribute 的 class,除了 [AuditInclude] 属性外,它还可以添加到任何模型中。为了便于阅读,我还创建了一个名为 AuditActionOptions 的枚举。

/// <summary>
/// Used with OptIn AnnotationMode to specify actions for auditing
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class AuditIncludeActionAttribute : Attribute
{
    public List<AuditActionOptions> AuditActions;

    public AuditIncludeActionAttribute(AuditActionOptions auditActionOption)
    {
        this.AuditActions = new List<AuditActionOptions>();
        this.AuditActions.Add(auditActionOption);
    }

    public AuditIncludeActionAttribute(AuditActionOptions auditActionOption1, AuditActionOptions auditActionOption2)
    {
        this.AuditActions = new List<AuditActionOptions>();
        this.AuditActions.Add(auditActionOption1);
        this.AuditActions.Add(auditActionOption2);
    }
}

public enum AuditActionOptions
{
    Insert = 1,
    Update = 2,
    Delete = 3        
} 

然后我将以下代码添加到设置中以确定是否应审核数据。在我的用例中,性能是可以接受的。

    Audit.Core.Configuration.Setup()
        .UseEntityFramework(ef => ef
            .AuditTypeMapper(t => typeof(AuditLog))
            .AuditEntityAction<AuditLog>((ev, entry, entity) =>
            {
                entity.AuditData = entry.ToJson();
                entity.AuditDate = DateTime.Now;
                entity.AuditUserId = SessionHelper.UserKey;
                entity.TablePk = entry.PrimaryKey.First().Value.ToString();
                entity.Duration = ev.Duration;
                entity.Action = entry.Action;
                entity.SchemaName = entry.Schema;
                entity.TableName = entry.Table;
                var entityFrameworkEvent = ev.GetEntityFrameworkEvent();
                entity.TransactionId = entityFrameworkEvent.TransactionId;

                AuditIncludeActionAttribute actionAttribute = (AuditIncludeActionAttribute)Attribute.GetCustomAttribute(entry.EntityType, typeof(AuditIncludeActionAttribute));

                // If there is no AuditIncludeActionAttribute then the entity only has the AuditInclude attribute and should log all actions
                if (actionAttribute == null)
                {
                    return true;
                }
                else if (entry.Action.ToUpper() == "DELETE" && actionAttribute.AuditActions.Contains(AuditActionOptions.Delete))
                {
                    return true;
                }
                else if (entry.Action.ToUpper() == "INSERT" && actionAttribute.AuditActions.Contains(AuditActionOptions.Insert))
                {
                    return true;
                }
                else if (entry.Action.ToUpper() == "UPDATE" && actionAttribute.AuditActions.Contains(AuditActionOptions.Update))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            })
        .IgnoreMatchedProperties(true));