Search code examples
c#asp.net-coreconfigurationaspnetboilerplateaudit-logging

Configure audit logging for Application Services in ASP.NET Boilerplate


I am trying to determine if there is a way to remove the Audit Logging that is provided by default in the ASP.NET Boilerplate code.

Looking at the documentation, it appears that removing the selector from the Auditing configuration with the following should work, but it doesn't.

Configuration.Auditing.Selectors.Clear()

The reason being, say, if I wanted to leave auditing enabled, but only wanted to audit certain services rather than all the services of type IApplicationService.

I have tried to put the above line in various modules, all with no success. All service calls are being logged in the AbpAuditLogs table.


Solution

  • Background

    ASP.NET Boilerplate provides the infrastructure to create application services.

    The CreateControllersForAppServices method gets an assembly and converts all the application services to MVC controllers in that assembly.

    ABP defines some pre-built filters for ASP.NET Core. All of them are added to all actions of all controllers by default.

    https://aspnetboilerplate.com/Pages/Documents/AspNet-Core

    Problem

    Configuration.Auditing.Selectors.Clear() handles it for AuditingInterceptor, not action filters.

    AbpAuditActionFilter passes defaultValue as true to _auditingHelper.ShouldSaveAudit(...).

    The defaultValue is eventually returned by AuditingHelper.

    Workaround

    We cannot replace AbpAuditActionFilter easily, but we can replace AuditingHelper:

    1. Copy AuditingHelper and rename it to IgnoreDefaultAuditingHelper.

    2. Modify the last line of AuditingHelper.ShouldSaveAudit to ignore defaultValue:

      public bool ShouldSaveAudit(MethodInfo methodInfo, bool defaultValue = false)
      {
          // ...
      
          return false;
          // return defaultValue;
      }
      
    3. Replace IAuditingHelper in the PreInitialize method of your module:

      public override void PreInitialize()
      {
          Configuration.ReplaceService<IAuditingHelper, IgnoreDefaultAuditingHelper>();
      }