Open Closed

Enabling Audit for ABP 9.1.1 #10211


User avatar
0
sinancondo created

We are planning to enable audit on our app , but we want to use a separate MSSQL DB to store the audit data. What is the best recommended way to setup a new separate database , and do we need to implement a custom IAuditingStore store just because we use a different database ? there is a way to pass the connection string of the other database ? thanks


1 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    1. Solution
    • You do not need a custom IAuditingStore just because you want to persist audit logs into a different SQL Server database. ABP’s EF Core integration already supports multiple databases and separate DbContexts per module.

    • Recommended approach (ABP 9.1.1):

      1. Add the Audit Logging EF Core module to your EFCore layer:
    [DependsOn(
          typeof(AbpEntityFrameworkCoreSqlServerModule),
          typeof(AbpEntityFrameworkCoreModule),
          typeof(AbpAuditLoggingEntityFrameworkCoreModule) // important
      )]
      public class YourAppEntityFrameworkCoreModule : AbpModule
      {
          public override void ConfigureServices(ServiceConfigurationContext context)
          {
              Configure<AbpDbContextOptions>(options =>
              {
                  // Default DB for your main DbContext(s)
                  options.Configure<YourAppDbContext>(c => c.UseSqlServer());
    
                  // Configure the audit logging DbContext to use a different connection string
                  options.Configure<AbpAuditLoggingDbContext>(c =>
                  {
                      c.UseSqlServer();
                  });
    
                  // Fallback provider if needed
                  options.UseSqlServer();
              });
    
              // Map connection strings by name (Default vs. AuditLogging)
              // appsettings.json:
              // "ConnectionStrings": {
              //   "Default": "Server=...;Database=MainDb;...",
              //   "AuditLogging": "Server=...;Database=AuditDb;..."
              // }
              Configure<AbpDbConnectionOptions>(opt =>
              {
                  // Tell ABP to use "AuditLogging" connection for AbpAuditLoggingDbContext
                  opt.Databases.Configure("AuditLogging", database =>
                  {
                      database.MappedConnections.Add(typeof(AbpAuditLoggingDbContext));
                  });
              });
          }
      }
    
    1. Set up connection strings in appsettings.json:
    • In your HttpApi.Host (and any process that writes audit logs):
    {
        "ConnectionStrings": {
          "Default": "Server=.;Database=MainDb;Trusted_Connection=True;",
          "AuditLogging": "Server=.;Database=AuditDb;Trusted_Connection=True;"
        }
      }
    
    1. Ensure the audit tables are created in the separate DB:
    • Include the AbpAuditLoggingDbContext in your migration process so that:
      • AbpAuditLogs
      • AbpAuditLogActions
      • AbpEntityChanges
      • AbpEntityPropertyChanges are created in the AuditDb. Run migrations for the audit logging DbContext (or use ABP’s consolidated migrator if you already use it and it’s configured to handle multiple databases).
    1. Enable auditing as usual:
    Configure<AbpAuditingOptions>(options =>
      {
          options.IsEnabled = true;
          options.EntityHistorySelectors.AddAllEntities();
          // etc.
      });
    
      public override void OnApplicationInitialization(ApplicationInitializationContext context)
      {
          var app = context.GetApplicationBuilder();
          app.UseAuditing(); // required
      }
    
    • Why no custom IAuditingStore is required:

      • The default AuditingStore provided by the Volo.Abp.AuditLogging module persists audit logs via IAuditLogRepository, which is backed by AbpAuditLoggingDbContext. By mapping AbpAuditLoggingDbContext to a different connection string (AuditLogging), the default store will automatically save to that separate database.
    • Optional: schema customization

      • If you want to change the schema for audit tables, set it early in Program.cs (or module pre-config) of all projects that reference AbpAuditLogging EFCore:
    AbpAuditLoggingDbProperties.DbSchema = "audit";
    

    Ensure your migrations align with the chosen schema.

    1. Sources
    • Validated: Entity Framework Core integration and multiple databases:
      • https://docs.abp.io/en/abp/latest/Entity-Framework-Core
    • Validated: Support guidance showing provider configuration for AbpAuditLoggingDbContext and auditing middleware:
      • https://abp.io/support/questions/6880

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.1.0-preview. Updated on December 12, 2025, 10:36
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.