简体   繁体   English

Audit.NET - 实体框架 - 根据操作过滤审核事件

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

I'm in the process of implementing Auditing with Audit.NET and Entity Framework with an MVC application running .NET 4.6.1.我正在使用运行 .NET 4.6.1 的 MVC 应用程序使用 Audit.NET 和实体框架实施审计。

I have a requirement to be able to restrict auditing based on the database operation being performed.我需要能够根据正在执行的数据库操作来限制审计。 I've reviewed the documentation and see that I can control this via the AuditEntityAction by writing custom code.我查看了文档,发现我可以通过编写自定义代码通过 AuditEntityAction 来控制它。

What I'm looking for is the ability to attribute my data models to indicate which operations should be audited.我正在寻找的是能够将我的数据模型归因于指示应该审计哪些操作。 I can create this logic if it doesn't exist but thought I'd check with the dev community first.如果它不存在,我可以创建这个逻辑,但我想我会先咨询开发社区。 Below is an example of what I'm looking for.下面是我正在寻找的一个例子。

[AuditInclude("DELETE")]

or或者

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

You can make the function AuditEntityAction to return a boolean to indicate whether to include (true) or ignore (false) the entity on the output log.您可以使 function AuditEntityAction返回 boolean 以指示是在 output 日志中包含(真)还是忽略(假)该实体。

For example:例如:

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));

I accepted "thepirate000" answer as it provided me with the solution, but ultimately I did the implementation slightly different.我接受了“thepirate000”答案,因为它为我提供了解决方案,但最终我的实现略有不同。 I created a class called AuditIncludeActionAttribute which can be added to any models in addition to the [AuditInclude] attribute.我创建了一个名为 AuditIncludeActionAttribute 的 class,除了 [AuditInclude] 属性之外,它还可以添加到任何模型中。 I also created an enumeration called AuditActionOptions for readability.为了便于阅读,我还创建了一个名为 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        
} 

I then added the following code to the setup to determine if the data should be audited.然后,我将以下代码添加到设置中以确定是否应审核数据。 The performance was acceptable in my use case.在我的用例中,性能是可以接受的。

    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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM