Open Closed

Add Volo.Abp.AuditLogging to microservice template #6880


User avatar
0
paykoolbackend created
  • ABP Framework version: v7.1.1
  • UI Type:
  • Database System: SQL Server
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes
  • Exception message and full stack trace:
* An exception was thrown while activating Volo.Abp.AuditLogging.EntityFrameworkCore.AbpAuditLoggingDbContext -> λ:Microsoft.EntityFrameworkCore.DbContextOptions`1[[Volo.Abp.AuditLogging.EntityFrameworkCore.AbpAuditLoggingDbContext, Volo.Abp.AuditLogging.EntityFrameworkCore, Version=7.1.1.0, Culture=neutral, PublicKeyToken=null]].
Autofac.Core.DependencyResolutionException: An exception was thrown while activating Volo.Abp.AuditLogging.EntityFrameworkCore.AbpAuditLoggingDbContext -> λ:Microsoft.EntityFrameworkCore.DbContextOptions`1[[Volo.Abp.AuditLogging.EntityFrameworkCore.AbpAuditLoggingDbContext, Volo.Abp.AuditLogging.EntityFrameworkCore, Version=7.1.1.0, Culture=neutral, PublicKeyToken=null]].
 ---> Volo.Abp.AbpException: No configuration found for Microsoft.EntityFrameworkCore.DbContext, Microsoft.EntityFrameworkCore, Version=7.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60! Use services.Configure<AbpDbContextOptions>(...) to configure it.
   at Volo.Abp.EntityFrameworkCore.DependencyInjection.DbContextOptionsFactory.Configure[TDbContext](AbpDbContextOptions options, AbpDbContextConfigurationContext`1 context)
   at Volo.Abp.EntityFrameworkCore.DependencyInjection.DbContextOptionsFactory.Create[TDbContext](IServiceProvider serviceProvider)
   at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Activators.Delegate.DelegateActivator.<ConfigurePipeline>b__2_0(ResolveRequestContext ctxt, Action`1 next)
   at Autofac.Core.Resolving.Middleware.DisposalTrackingMiddleware.Execute(ResolveRequestContext context, Action`1 next)   at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   --- End of inner exception stack trace ---
   at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Middleware.SharingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Middleware.CircularDependencyDetectorMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request)
   at Autofac.Core.Resolving.ResolveOperation.ExecuteOperation(ResolveRequest request)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.CreateDbContextAsync(IUnitOfWork unitOfWork)
   at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.CreateDbContextAsync(IUnitOfWork unitOfWork, String connectionStringName, String connectionString)
   at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.GetDbContextAsync()
   at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.InsertAsync(TEntity entity, Boolean autoSave, CancellationToken cancellationToken)
   at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)
   at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
   at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
   at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
   at Volo.Abp.AuditLogging.AuditingStore.SaveLogAsync(AuditLogInfo auditInfo)
   at Volo.Abp.AuditLogging.AuditingStore.SaveAsync(AuditLogInfo auditInfo)
  • Steps to reproduce the issue:
  • Dear Supports,

I am trying to add abp auditlogging service to the microservice template project. I have manually created the migrations to add the following tables: [AbpAuditLogs] [AbpAuditLogActions] [AbpEntityChanges] [AbpEntityPropertyChanges]

then, In application layer, added

&lt;PackageReference Include=&quot;Volo.Abp.AuditLogging.Application&quot; Version=&quot;7.1.1&quot; /&gt;

and xxxApplicationModule.cs

Configure&lt;AbpAuditingOptions&gt;(options =>
{
    options.EntityHistorySelectors.AddAllEntities();
});

In domain.shared layer, added

&lt;PackageReference Include=&quot;Volo.Abp.AuditLogging.Domain.Shared&quot; Version=&quot;7.1.1&quot; /&gt;

xxxDomainSharedModule.cs added

[DependsOn(
    ....
    typeof(AbpAuditLoggingDomainSharedModule)
)]

In EntityFrameworkCore layer, added

&lt;PackageReference Include=&quot;Volo.Abp.AuditLogging.EntityFrameworkCore&quot; Version=&quot;7.1.1&quot; /&gt;

DbContext.cs added

builder.ConfigureAuditLogging();

In HttpApi, HttpApi.Client and HttpApi.Host layer, the reference is also added.

I would like to know how to configure the Dbcontext as it shows No configuration found?


11 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Do you depend on the AbpAuditLoggingEntityFrameworkCoreModule on the EntityFrameworkCore layer?

  • User Avatar
    0
    paykoolbackend created

    hi

    Do you depend on the AbpAuditLoggingEntityFrameworkCoreModule on the EntityFrameworkCore layer?

    Yes.

    [DependsOn(
        typeof(AbpEntityFrameworkCoreSqlServerModule),
        typeof(AbpEntityFrameworkCoreModule),
        typeof(AbpAuditLoggingEntityFrameworkCoreModule)
    )]
    
    
    

    In ConfigureServices

    Configure< AbpDbContextOptions>(options =>
    {
        options.Configure< MyServiceDbContext>(c =>
        {
            c.UseSqlServer(b =>
            {
                b.MigrationsHistoryTable("__MyService_Migrations", MyServiceDbProperties.DbSchema);
            });
        });
    });
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    options.UseSqlServer(); // add this line.

    See https://docs.abp.io/en/abp/latest/Entity-Framework-Core#abpdbcontextoptions

    
    Configure<AbpDbContextOptions>(options =>
    {
        options.Configure<MyServiceDbContext>(c =>
        {
            c.UseSqlServer(b =>
            {
                b.MigrationsHistoryTable("__MyService_Migrations", MyServiceDbProperties.DbSchema);
            });
        });
        options.UseSqlServer(); // add this line.
    });
    

    or

    
    Configure<AbpDbContextOptions>(options =>
    {
        options.Configure<MyServiceDbContext>(c =>
        {
            c.UseSqlServer(b =>
            {
                b.MigrationsHistoryTable("__MyService_Migrations", MyServiceDbProperties.DbSchema);
            });
        });
    
        options.Configure<AbpAuditLoggingDbContext>(c =>
        {
            c.UseSqlServer...
        });
    });
    
  • User Avatar
    0
    paykoolbackend created

    options.UseSqlServer();

    Thank you so much. It resolved the error. However, when doing CRUD of the app service function, there is no record inserted to the AuditLog related tables. What else do I need to configure?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    UseAuditing() middleware should be added to the ASP.NET Core request pipeline in order to create and save the audit logs. If you've created your applications using the startup templates, it is already added.

    https://docs.abp.io/en/abp/latest/Audit-Logging#useauditing

  • User Avatar
    0
    paykoolbackend created

    UseAuditing()

    hi, yes I have added this configuration and I found the log is actually inserted into the table with schema dbo dbo.[AbpAuditLogs] dbo.[AbpAuditLogActions] dbo.[AbpEntityChanges] dbo.[AbpEntityPropertyChanges]

    Can I customize the schema of these tables? who to override AbpCommonDbProperties?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can change this static variable in Program.cs of all projects

    AbpAuditLoggingDbProperties.DbSchema = "xxx"
    
  • User Avatar
    0
    paykoolbackend created

    hi

    You can change this static variable in Program.cs of all projects

    AbpAuditLoggingDbProperties.DbSchema = "xxx" 
    

    As I am using the microservice templeate, only HttpApi.Host contains the program.cs, add the static variable in here in enough?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    May not be enough, and other services may get a table not found error. You'd better change this in all projects that reference or indirectly reference to AbpAuditLoggingEntityFrameworkCoreModule

  • User Avatar
    0
    paykoolbackend created

    hi

    May not be enough, and other services may get a table not found error. You'd better change this in all projects that reference or indirectly reference to AbpAuditLoggingEntityFrameworkCoreModule

    hi, could you provide some examples of the implementation? I am not sure where and how to add this

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can ignore this for now. Try to test if the AuditLogging works.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15