myesn

myEsn2E9

hi
github

ABP: Audit Logging

image

如果使用 EF Core 存储,Abp 的审计日志最终会存储到下面 4 个类型对应的表里面:
AuditLog、AuditLogAction、EntityChanges、EntityPropertyChanges 属性说明参考:https://docs.abp.io/en/abp/latest/Audit-Logging#audit-log-object

官方中写的很详细了,本文只记录需要注意的点:

  1. 默认不会对 HTTP Get 请求记录审计日志,开启配置 AbpAuditingOptions..IsEnabledForGetRequests = true;
  2. 修改 ApplicationName 配置 `AbpAuditingOptions..ApplicationName ="";``
  3. 默认禁用实体变更记录,也就是 AbpEntityChangesAbpEntityPropertyChanges 两张表不会有任何记录,开启参考:https://docs.abp.io/en/abp/latest/Audit-Logging#entity-history-selectors
  4. 忽略指定网址路径的审计日志记录,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#abpaspnetcoreauditingoptions
  5. 默认开启审计日志的地方:所有 controller actions 的请求日志,Get 请求除外,参考 AbpAuditingOptions.IsEnabledForGetRequests;所有 Application Service 的调用日志,前提是接口和服务必须继承 IApplicationServiceApplicationService 才称为 Application Service
  6. 默认记录所有 controller actions 的请求日志
  7. 在 controller 或 controller actions 上添加 [DisableAuditing] 特性禁用审计日志,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#enable-disable-for-controllers-actions
  8. 在 Application Service class 或 class methods 上添加 [DisableAuditing] 特性禁用审计日志,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#enable-disable-for-application-services-methods
  9. 为其他任何服务启用 / 禁用审计日志,添加 [Audited] and [DisableAuditing] 特性即可,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#enable-disable-for-other-services
  10. 为某个实体和实体属性启用 / 禁用审计日志,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#enable-disable-for-entities-properties
  11. 自定义审计日志保存实现,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#iauditingstore
  12. 修改审计日志记录的属性值,注入 IAuditingManager 接口修改,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#iauditlogscope-iauditingmanager
  13. 如果想在 Background Worker 中记录日志,不需要为其添加 [Audited] 标记,因为定时任务不会经过 asp.net core 管道,就不会被创建 Audit Log Scope,那么这个特性就没用了,解决办法是手动创建 AuditLogScope,当手动创建时,默认只会添加 AuditLog 记录,不会为其添加 AuditLogAction 记录,需要自己手动 Add,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#manually-create-an-audit-log-scope

在 MiaoXinWebModule.cs 中配置 AbpAuditingOptions:

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        ...
        ConfigureAuditing();
    }

    private void ConfigureAuditing()
    {
        // 配置说明:https://docs.abp.io/en/abp/latest/Audit-Logging#abpauditingoptions
        //Configure<AbpAuditingOptions>(options =>
        //{
        //    // HTTP Get 请求日志记录,默认 false,正常来说 Get 请求不应对数据库进行任何更改,因此也不需要记录审计日志。 
        //    //options.IsEnabledForGetRequests = true;

        //    // 保存所有 Entity 的更改记录将需要大量的数据库空间。因此,审计日志系统默认禁用此功能,除非显示配置它
        //    // 要保存所有实体的所有更改,只需使用 AddAllEntities() 扩展方法即可
        //    // 也可以调用 Add 函数来编写 lambda 表达式来定义筛选条件,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#entity-history-selectors
        //    //options.EntityHistorySelectors.AddAllEntities();
        //});

        // 如果需要忽略某个路径下的审计日志记录,参考:https://docs.abp.io/en/abp/latest/Audit-Logging#abpaspnetcoreauditingoptions
    }

BackGround Worker 代码参考:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Auditing;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Threading;
using Volo.Abp.Uow;

namespace ?.BackgroundWorkers
{
    /// <summary>
    /// 设置用户过期
    /// 
    /// https://docs.abp.io/zh-Hans/abp/latest/Background-Workers#asyncperiodicbackgroundworkerbase
    /// 
    /// AsyncPeriodicBackgroundWorkerBase 捕获并记录 由 DoWorkAsync 方法抛出的 异常.
    /// </summary>
    public class CustomerStatusCheckBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
    {
        public CustomerStatusCheckBackgroundWorker(
            AbpAsyncTimer timer,
            IServiceScopeFactory serviceScopeFactory
            )
            : base(timer, serviceScopeFactory)
        {
            // 每小时执行一次
            //Timer.Period = 1 * 60 * 60 * 1000;
            Timer.Period = 5 * 1000; // 5 秒执行一次测试
        }

        // https://docs.abp.io/en/abp/latest/Unit-Of-Work#unitofworkattribute
        // https://support.abp.io/QA/Questions/1753/SystemObjectDisposedException-Cannot-access-a-disposed-context-instance
        [UnitOfWork]
        protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
        {
            // 最好使用 PeriodicBackgroundWorkerContext 解析依赖 而不是构造函数.
            // 因为 AsyncPeriodicBackgroundWorkerBase 使用 IServiceScope 在你的任务执行结束时会对其 disposed.
            //Logger.LogDebug("Starting: CustomerStatusCheckBackgroundWorker");

            // 手动创建 Audit Log Scope:https://docs.abp.io/en/abp/latest/Audit-Logging#manually-create-an-audit-log-scope
            var auditingManager = workerContext.ServiceProvider.GetRequiredService<IAuditingManager>();
            using (var auditingScope = auditingManager.BeginScope())
            {
                auditingManager.Current.Log.Url = "BackgroundWorker-CustomerStatusCheck";
                try
                {
					// do somethings..

                    auditingManager.Current.Log.Comments.Add($"一些适当的注释");
                }
                catch (Exception ex)
                {
                    //Add exceptions
                    auditingManager.Current.Log.Exceptions.Add(ex);
                    throw;
                }
                finally
                {
                    // Always save the log
                    await auditingScope.SaveAsync();
                }
            }

            //Logger.LogDebug("Completed: CustomerStatusCheckBackgroundWorker");
        }
    }
}

参考#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.