ASPNET Boilerplate,扩展审核日志 [英] ASPNET Boilerplate, extending audit log

查看:63
本文介绍了ASPNET Boilerplate,扩展审核日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to extend the AuditLog entity in ASPNETBOILETPLATE framework in order to add some new properties to it. I have tried to extend the AuditLog class (ExtendedAuditInfo) and implement a customised version of AuditStore Class (ExtendedAuditStore). However, I am not able to inject my new ExtendedAuditInfo in the constructor and receive two error messages regarding unmatching input parameters in the Constructor and SaveAsync method.

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>'

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

上面的过程基于我发现此处

The procedure above is based on the idea that I found Here

推荐答案

我找到了基于为了扩展 AuditLog 类,必须使用继承.因此,需要从 AuditLog 继承一个新类,例如 ExtendedAuditInfo .

In order to extend the AuditLog class, inheritance must be used. Therefore a new class, let's say ExtendedAuditInfo needs to be inherited from 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
    }

该类必须添加到上下文中,并且显然,需要运行新的迁移才能添加新的属性.

This class has to be added to the context and obviously, a new migration needs to be run in order to add the 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 必须作为构造函数的第二个参数注入,并且可以用于插入扩展实体.

Finally, in the ExtendedAuditStore class, IRepository<ExtendedAuditLog, long> _extendedAuditLogRepository has to be injected as a second parameter of the constructor and can be used to insert the extended entity.

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

此外,代替从AuditingStore继承,还可以创建IAuditingStore的新实现并将其注入到应用程序服务中.

Also, instead of inheriting from AuditingStore, a new implementation for IAuditingStore can be created and injected into application services.

更新:

最后,您所需要做的就是替换 StartUp 类中的默认 AuditingStore :

Finally, all you need is to replace the default AuditingStore in StartUp class:

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

这篇关于ASPNET Boilerplate,扩展审核日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆