这是《ABP大型项目实战》系列文章的一篇。
 
项目发布到生产环境后难免会有错误。
那么如何进行调试和排错呢?
 
我看到俱乐部里有人是直接登陆生产服务器把数据库下载到开发机器进行调试排错。
这种办法是不适用于大型项目的:
  1. 首先,大型项目(特别是全球都有分公司的大型项目)很有可能24小时都有人在使用。所以尽量避免直接登录生产服务器操作,就算部署,也应该用DevOps、蓝绿部署等办法。
  2. 另外,如果大型项目有采用金丝雀发布和A/B测试,那么把数据库下载到开发机器这种方法是很不适用的。
  3. 即使大型项目没有采用金丝雀发布和A/B测试,也不适合把数据库下载到开发机器进行调试排错。因为数据库有可能很大,网络传输需要时间,特别是连VPN的时候,甚至有可能要从欧洲传到中国,又要从中国回传到欧洲。
  4. 生产环境数据库下载后为了安全还需要脱敏,这也需要时间。
 
还有其他方法,但是这些方法都存在一个问题,就是时光不能倒流,很多时候你是不可能叫客户回来重现一遍操作流程来复现bug的。
 
那么有什么好办法呢?
有的,通过查看日志来调试与排错。
 
ABP在这方面做得不错,内置了审计日志,提供了详细日志基类。嗯,这是两块内容了,一篇文章是讲不完的,所以我分开多篇文章来讲。
先从审计日志开始吧。
 
不得不说,ABP在这方面做得很好,审计日志是透明的,你要关闭审计日志反而要写代码控制。当然,这里不建议关闭审计日志。
 
然而,ABP的审计日志只提供了写,没有提供读的UI,这样的话,要看审计日志就必须要打开数据库查看了。
从前面的描述看到,这种做法在大型项目肯定是行不通的啦。我们必须要提供读取审计日志的UI。这就是这篇文章的主题。
 
在我使用的ABP 3.4版本里面,并没有提供审计日志的读取AppService, 但是提供了审计日志的Entity class。(注意: ABP更新很频繁,所以你目前使用的版本有可能新增甚至删除了部分interface或class)
所以我们第一步是先围绕ABP提供的审计日志Entity class(AuditLog)来建立AppService class和相关读取Mehtod. 以下是ABP 3.4版本的示例代码:
 注意:这是示例代码,直接复制到你的项目里不经任何修改很大概率是编译不通过的,你必须要根据你的项目实际情况进行修改,你最起码要改命名空间吧。
注意:ABP自己已经提供了AuditLog实体类,我们不需要另外再新建实体类了。
IAuditLogAppService.cs
 public interface IAuditLogAppService : IApplicationService
{
/// <summary>
/// 大型项目的审计日志量会十分大,所以最起码要分页
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<PagedResultDto<AuditLogListDto>> GetAuditLogs(GetAuditLogsInput input);
/// <summary>
/// 一定要提供Excel下载功能,一般建议是按照时间段选取
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<FileDto> GetAuditLogsToExcel(GetAuditLogsInput input);
/// <summary>
/// 提供全部审计日志的Excel下载,因为数据量会比较大,需要在服务器先压缩好,再提供给客户端下载。
/// </summary>
/// <returns></returns>
Task<FileDto> GetAuditLogsToExcel(); //List<AuditLogListDto> GetAllAuditLogs(); //错误案例示范,大型项目的审计日志量会十分大,所以最起码要分页
}

  AuditLogListDto.cs

using System;
using Abp.Application.Services.Dto;
using Abp.Auditing;
using Abp.AutoMapper; [AutoMapFrom(typeof(AuditLog))]
public class AuditLogListDto : EntityDto<long>
{
public long? UserId { get; set; } public string UserName { get; set; } public int? ImpersonatorTenantId { get; set; } public long? ImpersonatorUserId { get; set; } public string ServiceName { get; set; } public string MethodName { get; set; } public string Parameters { 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 string Exception { get; set; } public string CustomData { get; set; }
}

  AuditLogAppService.cs

[DisableAuditing] //屏蔽这个AppService的审计功能
[AbpAuthorize(AppPermissions.Pages_Administration_AuditLogs)]
public class AuditLogAppService : DemoAppServiceBase, IAuditLogAppService
{
private readonly IRepository<AuditLog, long> _auditLogRepository;
private readonly IRepository<User, long> _userRepository;
private readonly IAuditLogListExcelExporter _auditLogListExcelExporter;
private readonly INamespaceStripper _namespaceStripper; public AuditLogAppService(
IRepository<AuditLog, long> auditLogRepository,
IRepository<User, long> userRepository,
IAuditLogListExcelExporter auditLogListExcelExporter,
INamespaceStripper namespaceStripper)
{
_auditLogRepository = auditLogRepository;
_userRepository = userRepository;
_auditLogListExcelExporter = auditLogListExcelExporter;
_namespaceStripper = namespaceStripper;
} // 下面视具体业务情况实现接口的方法
}

  以下是使用EF来查询Auditlog关联User表数据的示例代码:

 IQueryable<AuditLogAndUser> query = from auditLog in _auditLogRepository.GetAll()
join user in _userRepository.GetAll() on auditLog.UserId equals user.Id into userJoin
from joinedUser in userJoin.DefaultIfEmpty()
where auditLog.ExecutionTime >= input.StartDate && auditLog.ExecutionTime <= input.EndDate
select new AuditLogAndUser { AuditLog = auditLog, User = joinedUser }; query = query
//.WhereIf(!input.UserName.IsNullOrWhiteSpace(), item => item.User.UserName.Contains(input.UserName))// 以前的写法,不支持多个用户名查询
.WhereIf(usernamelist != null, item => usernamelist.Contains(item.User.UserName))
//.WhereIf(!input.RealUserName.IsNullOrWhiteSpace(), item => item.User.Name.Contains(input.RealUserName))// 以前的写法,不支持多个用户名查询
.WhereIf(realusernamelist != null, item => realusernamelist.Contains(item.User.Name))
.WhereIf(!input.ServiceName.IsNullOrWhiteSpace(), item => item.AuditLog.ServiceName.Contains(input.ServiceName))
.WhereIf(!input.MethodName.IsNullOrWhiteSpace(), item => item.AuditLog.MethodName.Contains(input.MethodName))
.WhereIf(!input.BrowserInfo.IsNullOrWhiteSpace(), item => item.AuditLog.BrowserInfo.Contains(input.BrowserInfo))
.WhereIf(input.MinExecutionDuration.HasValue && input.MinExecutionDuration > , item => item.AuditLog.ExecutionDuration >= input.MinExecutionDuration.Value)
.WhereIf(input.MaxExecutionDuration.HasValue && input.MaxExecutionDuration < int.MaxValue, item => item.AuditLog.ExecutionDuration <= input.MaxExecutionDuration.Value)
.WhereIf(input.HasException == true, item => item.AuditLog.Exception != null && item.AuditLog.Exception != "")
.WhereIf(input.HasException == false, item => item.AuditLog.Exception == null || item.AuditLog.Exception == "");

这里可以看到,既有大量数据又有多表关联查询哦,并且纯是使用EF去做,实践证明EF性能并不差,顺便推广一下Edi的另一篇文章《Entity Framework 的一些性能建议

然而还是有同学强烈要求提供SQL版本,好吧,以下是最简单的sql版本:

select * from AbpAuditLogs left join AbpUsers on (AbpAuditLogs.UserId = AbpUsers.Id)
where AbpAuditLogs .ExecutionTime >= '2019/2/18' and AbpAuditLogs.ExecutionTime <= '2019/2/19'

这里还有个小技巧可以节省时间:

  1. 先手动建立一个AuditLog类

        public class AuditLog
    {
    /// <summary>
    /// TenantId.
    /// </summary>
    public virtual int? TenantId { get; set; } /// <summary>
    /// UserId.
    /// </summary>
    public virtual long? UserId { get; set; } /// <summary>
    /// Service (class/interface) name.
    /// </summary>
    public virtual string ServiceName { get; set; } /// <summary>
    /// Executed method name.
    /// </summary>
    public virtual string MethodName { get; set; } /// <summary>
    /// Calling parameters.
    /// </summary>
    public virtual string Parameters { get; set; } /// <summary>
    /// Return values.
    /// </summary>
    public virtual string ReturnValue { get; set; } /// <summary>
    /// Start time of the method execution.
    /// </summary>
    public virtual DateTime ExecutionTime { get; set; } /// <summary>
    /// Total duration of the method call as milliseconds.
    /// </summary>
    public virtual int ExecutionDuration { get; set; } /// <summary>
    /// IP address of the client.
    /// </summary>
    public virtual string ClientIpAddress { get; set; } /// <summary>
    /// Name (generally computer name) of the client.
    /// </summary>
    public virtual string ClientName { get; set; } /// <summary>
    /// Browser information if this method is called in a web request.
    /// </summary>
    public virtual string BrowserInfo { get; set; } /// <summary>
    /// Exception object, if an exception occured during execution of the method.
    /// </summary>
    public virtual string Exception { get; set; } /// <summary>
    /// <see cref="AuditInfo.ImpersonatorUserId"/>.
    /// </summary>
    public virtual long? ImpersonatorUserId { get; set; } /// <summary>
    /// <see cref="AuditInfo.ImpersonatorTenantId"/>.
    /// </summary>
    public virtual int? ImpersonatorTenantId { get; set; } /// <summary>
    /// <see cref="AuditInfo.CustomData"/>.
    /// </summary>
    public virtual string CustomData { get; set; } }

      

  2. 选中这个类然后鼠标右键52abp代码生成器
  3. 生成后再把AppService接口和类改成上面的代码
  4. 删除第一步建立的AuditLog类。把引用修正为“Abp.Auditing”

这个小技巧大概可以节省你三十分钟时间吧。

 
第二步就是UI层面,因为审计日志数量会很大,查阅基本要靠搜索,所以要选一个查阅功能强大的Grid组件,根据我个人经验,推荐使用primeng的table组件。具体代码因为没有什么技术难点,我就不贴了。
关于其他成熟框架组件库和如何在angular中使用多个成熟控件框架,请参考我的另外一篇文章《如何用ABP框架快速完成项目(6) - 用ABP一个人快速完成项目(2) - 使用多个成熟控件框架
对了,前端一定要完成导出Excel格式功能哦,你会发觉这个功能实在是太赞了!
 
现在终于可以不用登陆生产服务器就可以查看审计日志了。
但是这只是开始!
因为一个大型项目里,审计日志的增长速度是惊人的,如果审计日志表和主数据库放在一起,是十分不科学的。
那么我们如何解决这个问题呢?敬请期待下一篇文章《ABP大型项目实战(2) - 调试与排错 - 日志 - 单独存储审计日志》
 
Q&A:
  1. 如何禁用具体某个接口的审计功能?
    答:在类头加上如下属性

    [DisableAuditing] //屏蔽这个AppService的审计功能
    [AbpAuthorize(AppPermissions.Pages_Administration_AuditLogs)]
    public class AuditLogAppService : GHITAssetAppServiceBase, IAuditLogAppService

ABP大型项目实战(2) - 调试与排错 - 日志 - 查看审计日志的更多相关文章

  1. ABP大型项目实战(1) - 目录

    前面我写了<如何用ABP框架快速完成项目>系列文章,讲述了如何用ABP快速完成项目.   然后我收到很多反馈,其中一个被经常问到的问题就是,“看了你的课程,发现ABP的优势是快速开发,那么 ...

  2. Java 18套JAVA企业级大型项目实战分布式架构高并发高可用微服务电商项目实战架构

    Java 开发环境:idea https://www.jianshu.com/p/7a824fea1ce7 从无到有构建大型电商微服务架构三个阶段SpringBoot+SpringCloud+Solr ...

  3. GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)

    GraphQL + React Apollo + React Hook 大型项目实战(32 个视频) GraphQL + React Apollo + React Hook 大型项目实战 #1 介绍「 ...

  4. Spark大型项目实战:电商用户行为分析大数据平台

    本项目主要讲解了一套应用于互联网电商企业中,使用Java.Spark等技术开发的大数据统计分析平台,对电商网站的各种用户行为(访问行为.页面跳转行为.购物行为.广告点击行为等)进行复杂的分析.用统计分 ...

  5. 项目实战8.2-Linux下Tomcat开启查看GC信息

    本文收录在Linux运维企业架构实战系列 转自https://www.cnblogs.com/along21/ 一.开启GC日志 1.在Tomcat 的安装路径下,找到bin/catalina.sh  ...

  6. ABP开发框架前后端开发系列---(7)系统审计日志和登录日志的管理

    我们了解ABP框架内部自动记录审计日志和登录日志的,但是这些信息只是在相关的内部接口里面进行记录,并没有一个管理界面供我们了解,但是其系统数据库记录了这些数据信息,我们可以为它们设计一个查看和导出这些 ...

  7. JAVA架构之单点登录 任务调度 权限管理 性能优化大型项目实战

    单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...

  8. 企业项目实战 .Net Core + Vue/Angular 分库分表日志系统 | 简单的分库分表设计

    前言 项目涉及到了一些设计模式,如果你看的不是很明白,没有关系坚持下来,写完之后去思考去品,你就会有一种突拨开云雾的感觉,所以请不要在半途感觉自己看不懂选择放弃,如果我哪里写的详细,或者需要修正请联系 ...

  9. 企业项目实战 .Net Core + Vue/Angular 分库分表日志系统 | 前言

    介绍 大家好我是初久,一名从业4年的.Net开发攻城狮,从今天开始我会和大家一起对企业开发中常用的技术进行分享,一方面督促自己学习,一方面也希望大家可以给我指点出更好的方案,我们一起进步. 项目背景 ...

随机推荐

  1. HTTP协议简介详解 HTTP协议发展 原理 请求方法 响应状态码 请求头 请求首部 java模拟浏览器客户端服务端

    协议简介 协议,自然语言里面就是契约,也是双方或者多方经过协商达成的一致意见; 契约也即类似于合同,自然有甲方123...,乙方123...,哪些能做,哪些不能做; 通信协议,也即是双方通过网络通信必 ...

  2. Zabbix3.0基础教程之二:item、trigger、action、graph配置

    一.Zabbix监控报警过程 在一次完整的Zabbix配置中,需要涉及到的术语有以下几项: 1.host groups:主机组,按生产需求将功能类别相近或相同的主机进行分组,便于管理. 2.host: ...

  3. 多线程Thread,线程池ThreadPool

    首先我们先增加一个公用方法DoSomethingLong(string name),这个方法下面的举例中都有可能用到 #region Private Method /// <summary> ...

  4. 大型网站架构演进(6)使用NoSQL和搜索引擎

    随着网站业务越来越复杂,对数据存储和检索的需求也越来越复杂,网站需要采用一些非关系型数据库技术(即NoSQL)和非数据库查询技术如搜索引擎.NoSQL数据库一般使用MongoDb,搜索引擎一般使用El ...

  5. 简单介绍Tomcat

    Tomcat是一个Web容器,或者说是Web服务器.用于管理和部署Web应用.还有一种服务器叫做应用服务器,它的功能比web服务器要强大的多,因为它可以部署EJB应用,可以实现容器管理的事务,一般的应 ...

  6. 记录自己使用GitHub的点点滴滴

    前言 现在大多数开发者都有自己的GitHub账号,很多公司也会以是否有GitHub作为一项筛选简历以及人才的选项了,可见拥有一个GitHub账号的重要性,本文就从最基本的GitHub账号的注册到基本的 ...

  7. 利用efi功能更改bios主板被隐藏的设置(如超频)

    整理自(来源): http://tieba.baidu.com/p/4934345324 ([新手教程]利用EFI启动盘修改 隐藏bios设置) http://tieba.baidu.com/p/49 ...

  8. 设计模式系列之单例模式(Singleton Pattern)

    单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式.这种模式涉及到一个单一的类,该类负责创建自己的对象 ...

  9. MongoDB数据库的设计规范

    MongoDB是非关系型数据库的典型代表,DB-Engines Ranking 数据显示,近年来,MongoDB在NoSQL领域一直独占鳌头.MongoDB是为快速开发互联网应用 而设计的数据库系统, ...

  10. 数据库H2学习

    本文转载自:https://www.cnblogs.com/xdp-gacl/p/4171024.html 一.H2数据库介绍 常用的开源数据库有:H2,Derby,HSQLDB,MySQL,Post ...