简体   繁体   English

Audit.NET - 实体框架 - 使用 AuditScope 基于业务操作的日志

[英]Audit.NET - Entity Framework - Log Based on Business Action using AuditScope

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'd like to be able to audit certain business actions that affect multiple entities while not auditing every modification to the entity.我希望能够审计影响多个实体的某些业务操作,而不是审计对实体的每一次修改。 For example, if a user is deleted then I want to log every table that will be modified.例如,如果一个用户被删除,那么我想记录每个将被修改的表。 In this environment, not all of the entities are set up with relationships so I can't just include the object graph.在这种环境中,并非所有实体都设置了关系,因此我不能只包含 object 图。 I don't want to log every modification to the Emails entity, just log this data when an employee is being deleted.我不想记录对电子邮件实体的所有修改,只需在删除员工时记录这些数据。

I thought I could use the AuditScope.Create() functionality to be able to handle this scenario but I'm not having any luck.我以为我可以使用 AuditScope.Create() 功能来处理这种情况,但我没有任何运气。

Currently, I configure Audit.NET on startup with the following code.目前,我在启动时使用以下代码配置 Audit.NET。

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

In my controller action, I'm trying to log the change via the AuditScope object.在我的 controller 操作中,我试图通过 AuditScope object 记录更改。 I want to be able to log modifications for any objects which are saved within the using.我希望能够记录使用中保存的任何对象的修改。

I've tried this a couple of different ways but have not had any luck.我已经尝试了几种不同的方法,但没有任何运气。 Employee is just a class I created to contain my various entities and is not an actual entity mapped to the database. Employee 只是我创建的一个 class 以包含我的各种实体,而不是映射到数据库的实际实体。

            var employee = new Employee();
            employee.Users = _db.Users.Where(x => x.EmpId == empid).ToList();
            employee.Emails = _db.Emails.Where(x => x.EmpId == empid).ToList();
            employee.Phones = _db.Phones.Where(x => x.EmpId == empid).ToList();

            using (AuditScope.Create("DeleteEmployee", () => employee))
            {
                _db.Users.RemoveRange(employee.Users);
                _db.Emails.RemoveRange(employee.Emails);
                _db.Phones.RemoveRange(employee.Phones);
                
                var EmployeeLog = new EmployeeLog
                {
                    ActionType = "Delete",
                    DeletedBy = ScopeUser.ID,
                    DeletedUser = employee.Users[0].UserName,
                    DeletedOn = DateTime.Now
                };

                _db.EmployeeLogs.Add(EmployeeLog);
                _db.SaveChanges();
            }    

Here's my Employee Class definition:这是我的员工 Class 定义:

[AuditInclude]
public class Employee
{
    public List<tbl_User> Users { get; set; }
    public List<tbl_Email> Emails { get; set; }
    public List<tbl_Phone> Phones { get; set; }
}

I also tried the following implementation without using the Employee class but nothing was logged for it either.我还尝试了以下实现而不使用 Employee class 但也没有记录任何内容。

var user = _db.Users.Where(x => x.EmpId == empid).ToList();
var emails = _db.Emails.Where(x => x.EmpId == empid).ToList();
var phones = _db.Phones.Where(x => x.EmpId == empid).ToList();

using (AuditScope.Create("DeleteEmployee", () => new { User = user, Emails = emails, Phones = phones }))
{
        _db.Users.RemoveRange(user);
        _db.Emails.RemoveRange(emails);
        _db.Phones.RemoveRange(phones);
        var EmployeeLog = new EmployeeLog
        {
             ActionType = "Delete",
             DeletedBy = ScopeUser.ID,
             DeletedUser = user[0].UserName,
             DeletedOn = DateTime.Now
         };

         _db.EmployeeLogs.Add(EmployeeLog);
         _db.SaveChanges();
}                

Other entities that have the [AuditInclude] attribute are logging as expected.具有 [AuditInclude] 属性的其他实体正在按预期进行记录。 Either my configuration is incorrect, you can't mix AuditScope and Audit.EntityFramework this way or AuditScope is not intended to be used to track multiple entities.要么我的配置不正确,要么不能以这种方式混合 AuditScope 和 Audit.EntityFramework,要么 AuditScope 不打算用于跟踪多个实体。 Any direction that can be provided would be greatly appreciated.任何可以提供的方向将不胜感激。

Note the EntityFramework Data Provider ( .UseEntityFramework() ) will only log the audit events generated by the Audit.EntityFramework library.请注意, EntityFramework Data Provider ( .UseEntityFramework() ) 只会记录Audit.EntityFramework库生成的审计事件。 It will discard any other audit event type.它将丢弃任何其他审计事件类型。

So you cannot make the Entity Framework Data Provider to log an arbitrary event created via AuditScope.Create() (unless you manually create an Audit.EntityFramework.AuditEventEntityFramework , but that makes little sense)因此,您不能让实体框架数据提供程序记录通过AuditScope.Create()创建的任意事件(除非您手动创建Audit.EntityFramework.AuditEventEntityFramework ,但这没什么意义)

Looks like you are logging everything to the same Audit Log table, so you can probably use the SQL Data Provider that will log any type of audit event.看起来您正在将所有内容记录到同一个审核日志表中,因此您可以使用SQL 数据提供程序来记录任何类型的审核事件。

If you need to use multiple data providers (EF logging using Audit.EF and its data provider, and also some other manual logging) check the following:如果您需要使用多个数据提供程序(使用 Audit.EF 及其数据提供程序进行 EF 日志记录,以及其他一些手动日志记录),请检查以下内容:

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

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