ASPNET 样板,扩展审计日志

ASPNET Boilerplate, extending audit log

我正在尝试扩展 ASPNETBOILETPLATE 框架中的 AuditLog 实体,以便向其添加一些新属性。我尝试扩展 AuditLog class (ExtendedAuditInfo) 并实现 AuditStore Class (ExtendedAuditStore) 的自定义版本。但是,我无法在构造函数中注入新的 ExtendedAuditInfo 并收到两条关于 ConstructorSaveAsync 方法中输入参数不匹配的错误消息。

Class ExtendedAuditInfo:

public class ExtendedAuditInfo : AuditInfo
{
    // Some properties
}

Class ExtendedAuditStore:

public class ExtendedAuditStore : AuditingStore
{
    public ExtendedAuditStore(IRepository<ExtendedAuditInfo, long> auditLogRepository)
        : base(auditLogRepository)
    {
    }

    public override Task SaveAsync(ExtendedAuditInfo auditInfo)
    {
        if (!string.IsNullOrEmpty(auditInfo.Parameters) && auditInfo.Parameters != "{}")
        {
            var parameters = JsonConvert.DeserializeObject<AuditParameterInput>(auditInfo.Parameters);
            if (parameters != null)
                auditInfo.CustomData = parameters.Input.Id.ToString();
        }

        return base.SaveAsync(auditInfo);
    }
}

错误是:

cannot convert from 'Abp.Domain.Repositories.IRepository<SixB.Serafina.Auditing.ExtendedAuditInfo, long>' to 'Abp.Domain.Repositories.IRepository<Abp.Auditing.AuditLog, long>'

no suitable method found to override

上面的过程是基于我发现的想法

根据How To Extend Existing Entities的官方文档找到了解决方案。

为了扩展AuditLog class,必须使用继承。因此,一个新的 class,比方说 ExtendedAuditInfo 需要继承自 AuditLog

public class ExtendedAuditLog : AuditLog
    {
        public ExtendedAuditLog()
        {

        }

        public ExtendedAuditLog(AuditInfo auditInfo)
        {
            this.BrowserInfo = auditInfo.BrowserInfo;
            this.ClientIpAddress = auditInfo.ClientIpAddress;
            this.ClientName = auditInfo.ClientName;
            this.CustomData = auditInfo.CustomData;
            this.Exception = auditInfo.Exception?.Message.ToString() + "";
            this.ExecutionDuration = auditInfo.ExecutionDuration;
            this.ExecutionTime = auditInfo.ExecutionTime;
            this.ImpersonatorTenantId = auditInfo.ImpersonatorTenantId;
            this.ImpersonatorUserId = auditInfo.ImpersonatorUserId;
            this.MethodName = auditInfo.MethodName;
            this.Parameters = auditInfo.Parameters;
            this.ReturnValue = auditInfo.ReturnValue;
            this.ServiceName = auditInfo.ServiceName;
            this.TenantId = auditInfo.TenantId;
            this.UserId = auditInfo.UserId;
        }

        //new properties
    }

必须将此 class 添加到上下文中,显然,新迁移需要 运行 才能添加新属性。

public class ProjectDbContext : AbpZeroDbContext<Tenant, Role, User, ProjectDbContext >
{
    /* Define a DbSet for each entity of the application */
    
    public SerafinaDbContext(DbContextOptions<SerafinaDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<County> Counties { get; set; }

    public virtual DbSet<Country> Countries { get; set; }

    public virtual DbSet<Currency> Currencies { get; set; }

    public virtual DbSet<OrganisationType> OrganisationTypes { get; set; }

    public virtual DbSet<ExtendedAuditLog> ExtendedAuditLogs { get; set; }
}

最后,在ExtendedAuditStore class中,IRepository<ExtendedAuditLog, long> _extendedAuditLogRepository必须作为构造函数的第二个参数注入,可用于插入扩展实体。

public class ExtendedAuditStore : AuditingStore
{
    IRepository<ExtendedAuditLog, long> _extendedAuditLogRepository;

    public ExtendedAuditStore(
        IRepository<AuditLog, long> auditLogRepository,
        IRepository<ExtendedAuditLog, long> extendedAuditLogRepository
        )
        : base(auditLogRepository)
    {
        _extendedAuditLogRepository = extendedAuditLogRepository;
    }

    public override async Task SaveAsync(AuditInfo auditInfo)
    {
        if (auditInfo.Exception != null)
            await base.SaveAsync(auditInfo);

        var auditLog = new ExtendedAuditLog(auditInfo);
        //new properties can be set here
        await _extendedAuditLogRepository.InsertAsync(auditLog);
    }
}

此外,可以创建 IAuditingStore 的新实现并将其注入到应用程序服务中,而不是从 AuditingStore 继承。

更新:

最后,您只需将StartUp中的默认AuditingStore替换掉class:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IAuditingStore, ExtendedAuditStore>();
}