ASPNET 样板,扩展审计日志 [英] ASPNET Boilerplate, extending audit log
问题描述
我正在尝试扩展 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.
Class 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
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
以上过程基于我在此处
推荐答案
我根据如何扩展现有实体.
为了扩展AuditLog
类,必须使用继承.因此,一个新类,假设 ExtendedAuditInfo
需要从 AuditLog
继承.
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
必须作为构造函数的第二个参数注入,可用于插入扩展实体.
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);
}
}
此外,可以创建 IAuditingStore 的新实现并将其注入应用程序服务,而不是从 AuditingStore 继承.
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 样板,扩展审计日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!