ASPNET样板,扩展审计日志

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

ExtendedAuditInfo

public class ExtendedAuditInfo : AuditInfo
{
    // Some properties
}

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

错误是:

无法从“Abp.Domain.Repositories.IRepository<SixB.Serafina.Auditing.ExtendedAuditInfo, long>”转换为“Abp.Domain.Repositories.IRepository<Abp.Auditing.AuditLog, long>”

找不到合适的方法来覆盖

上面的过程是基于我在这里找到的想法

回答

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

为了扩展AuditLog类,必须使用继承。因此,一个新类,假设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
    }

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

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类中,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 继承。

更新:

最后,所有你需要的是替换默认AuditingStoreStartUp类:

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


以上是ASPNET样板,扩展审计日志的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>