Open Closed

Add join table changes to a related entity's audit log history #8750


User avatar
0
ben.shelton created
  • ABP Framework version: v8.3.0
  • UI Type: MVC
  • Database System: EF Core
  • Tiered (for MVC) or Auth Server Separated (for Angular): no

Hello,

We have our User class that inherits FullAuditedEntity and a join table called UserAssignedMerchant that links the user to an assigned entity since the user can be linked to multiple ones. For context, the UserAssignedMerchant 's class inherits Entity instead of FullAuditedEntity. We would like to be able to show changes to the user's entries in UserAssignedMerchant on the user's audit log history. Is there a way that we can manually make the audit log entry when needed? Is there a better or different way we can accomplish this?

Thanks.


5 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, you can configure the entity history selector to conditionally log for a specific entity. Please check https://abp.io/docs/latest/framework/infrastructure/audit-logging#entity-history-selectors

    Regards.

  • User Avatar
    0
    ben.shelton created

    Hello,

    I looked over the documentation that you sent and tried using the Audited attribute, but this did not give me what I was looking for.

    I am trying to manually add a history log entry to our User entity for changes that are not occurring in the User table. The changes occur on our UserAssignedMerchant join table.

    Here is some additional context on our User implementation:

    • We implemented our own User class and dtos, but are using IdentityUserManager calls that we adapted from source code to set the properties to still use the built in AbpUsers table and features in our create and update methods.
    • Also during those methods, we set the entries in the join table with repository calls for the join table.
    • We give the UserAssignedMerchant entries to the end users in a field that is a Guid[] called AssignedMerchantIds, but this only on the dto not the class so it's manually set at the time of retrieval.

    Is there a repository call (such as through IAuditLogRepository) that we can make to insert a history log on User that will allow us to manually set what changed in AssignedMerchantIds? Or is there a better way to accomplish this?

    Thanks.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hello,

    I looked over the documentation that you sent and tried using the Audited attribute, but this did not give me what I was looking for.

    I am trying to manually add a history log entry to our User entity for changes that are not occurring in the User table. The changes occur on our UserAssignedMerchant join table.

    Here is some additional context on our User implementation:

    • We implemented our own User class and dtos, but are using IdentityUserManager calls that we adapted from source code to set the properties to still use the built in AbpUsers table and features in our create and update methods.
    • Also during those methods, we set the entries in the join table with repository calls for the join table.
    • We give the UserAssignedMerchant entries to the end users in a field that is a Guid[] called AssignedMerchantIds, but this only on the dto not the class so it's manually set at the time of retrieval.

    Is there a repository call (such as through IAuditLogRepository) that we can make to insert a history log on User that will allow us to manually set what changed in AssignedMerchantIds? Or is there a better way to accomplish this?

    Thanks.

    Hi, you can configure the AbpAuditingOptions and define an EntityHistorySelector as below:

                opt.EntityHistorySelectors.Add(
                    new NamedTypeSelector(
                        "UserAssignedMerchant",
                        type => typeof(UserAssignedMerchant).IsAssignableFrom(type) 
                        ));
    
  • User Avatar
    0
    ben.shelton created

    Hi, you can configure the AbpAuditingOptions and define an EntityHistorySelector as below:

                opt.EntityHistorySelectors.Add( 
                    new NamedTypeSelector( 
                        "UserAssignedMerchant", 
                        type => typeof(UserAssignedMerchant).IsAssignableFrom(type)  
                        )); 
    

    Hello,

    I implemented what you suggested, but I am not seeing a history record being created when I add or remove records from the UserAssignedMerchant table.

    For further context:

    • A record in the UserAssignedMerchant table consists of UserId, MerchantId, and TenantId
    • In the User Create and Update methods, we take the list of MerchantIds that are given to be assigned to the user in the respective dtos, then we create a record for each given merchant in the UserAssignedMerchant table if one doesn't already exist. Additionally, we delete records in the UserAssignedMerchant table that represent merchants the user no longer needs to be associated with.
  • User Avatar
    0
    ben.shelton created

    Hello,

    I re-examined my implementation of your suggestion and realized it was not making the associated history log due to us using the bulk calls, InsertManyAsync and DeleteManyAsync, on the repository to add/remove records in the UserAssignedMerchant table.

    I was able to modify our existing CustomBulkOperationProvider to address this and now it does make history logs when a record in the UserAssignedMerchant table is created or deleted as we need.

    Thanks.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 19, 2025, 10:09