博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NET - .NET Core 之 Abp AuditLog 将不同的Controller实体的审计日志存储到不同的Table
阅读量:4050 次
发布时间:2019-05-25

本文共 3576 字,大约阅读时间需要 11 分钟。

 

 

审计日志

: "审计跟踪(也称为审计日志)是一种安全相关的按时间顺序记录,记录集或记录目的和来源. 这种记录提供了在任何特定时间的操作,过程或事件产生影响活动顺序的文件证据 ".

ABP框架提供一个可扩展的审计日志系统,自动化的根据约定记录审计日志,并提供配置控制审计日志的级别.

 

IAuditingStore

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 DbSet
TestAudit1 { 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/

你可能感兴趣的文章
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
DIV/CSS:一个贴在左上角的标签
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>