本文共 3576 字,大约阅读时间需要 11 分钟。
: "审计跟踪(也称为审计日志)是一种安全相关的按时间顺序记录,记录集或记录目的和来源. 这种记录提供了在任何特定时间的操作,过程或事件产生影响活动顺序的文件证据 ".
ABP框架提供一个可扩展的审计日志系统,自动化的根据约定记录审计日志,并提供配置控制审计日志的级别.
IAuditingStore
是一个接口,用于保存ABP框架的审计日志对象(下面说明). 如果需要将审计日志对象保存到自定义数据存储中,可以在自己的应用程序中实现 IAuditingStore
并在替换.
如果没有注册审计存储,则使用 SimpleLogAuditingStore
. 它只是将审计对象写入标准.
已在中配置,它将审计日志对象保存到数据库中(支持多个数据库提供程序). 所以大多数时候你并不需要关心 IAuditingStore
是如何实现和使用的.
定义两个Table用于存储不同实体的审计日志:
public class TestAudit1 : AuditedEntity { public string ServiceName { get; set; } public string MethodName { get; set; } public string Parameters { get; set; } public string ReturnValue { get; set; } public DateTime ExecutionTime { get; set; } public int ExecutionDuration { get; set; } public string ClientIpAddress { get; set; } public string ClientName { get; set; } public string BrowserInfo { get; set; } //public Exception Exception { get; set; } public long? ImpersonatorUserId { get; set; } public int? ImpersonatorTenantId { get; set; } public string CustomData { get; set; } public static TestAudit1 CreateFromAuditInfo(AuditInfo auditInfo) { return new TestAudit1 { ServiceName = auditInfo.ServiceName, MethodName = auditInfo.MethodName, Parameters = auditInfo.Parameters, ReturnValue = auditInfo.ReturnValue, ExecutionTime = auditInfo.ExecutionTime, ExecutionDuration = auditInfo.ExecutionDuration, ClientIpAddress = auditInfo.ClientIpAddress, ClientName = auditInfo.ClientName, BrowserInfo = auditInfo.BrowserInfo, //Exception = auditInfo.Exception, ImpersonatorUserId = auditInfo.ImpersonatorUserId, ImpersonatorTenantId = auditInfo.ImpersonatorTenantId, }; } }
在DBContext中添加
public DbSetTestAudit1 { get; set; }public DbSet TestAudit2 { get; set; }
生成数据库迁移
Add-Migration xxxUpdate-Database
自定义AuditStore实现Abp的IAuditingStore,重写SaveAsync方法
public class DBAuditStore : IAuditingStore, ITransientDependency { private readonly IRepository_auditLogRepository; private readonly IRepository _auditLogRepository2; public DBAuditStore(IRepository auditLogRepository, IRepository auditLogRepository2) { _auditLogRepository = auditLogRepository; _auditLogRepository2 = auditLogRepository2; } public Task SaveAsync(AuditInfo auditInfo) { switch (auditInfo.ServiceName) { case "xxx.Web.Host.Controllers.HomeController": return _auditLogRepository.InsertAsync(TestAudit1.CreateFromAuditInfo(auditInfo)); case "xxx.xxx.JobSchedule.JobScheduleAppService": return _auditLogRepository2.InsertAsync(TestAudit2.CreateFromAuditInfo(auditInfo)); default: return _auditLogRepository.InsertAsync(TestAudit1.CreateFromAuditInfo(auditInfo)); } } }
在AbpModule类中将自定义AuditStore注入,并替换Abp自带的SimpleLogAuditingStore.
public override void PreInitialize() { Configuration.ReplaceService(typeof(IAuditingStore), () => { IocManager.IocContainer.Register( Component.For() .ImplementedBy () .LifestyleTransient() );});
参考:
转载地址:http://tmnci.baihongyu.com/