abp icon indicating copy to clipboard operation
abp copied to clipboard

Optimize EntityChange logs

Open developerMCSI opened this issue 1 year ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Im trying to log the entity changes only for specific properties of an entity. Following the documentation i have added the data annotation "DisableAuditing" to the entity and added the tag "Audited" to the properties within the entity that i wanted to track changes. When i update an instance of the entity without changing any "audited" properties it still creates a record in the "AbpEntityChanges" table but no records in "AbpEntityPropertyChanges" resulting in an empty record when i look in the "auditing-logs" page: image

Describe the solution you'd like

If there were no changes to the audited property of the entity (no "AbpEntityPropertyChanges" records created) it would be useful if it did not create a record for the "AbpEntityChanges" table.

One implementation of this can be achieved by changing the function SaveLogAsync of the class AuditingStore.cs (https://github.com/abpframework/abp/blob/dev/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditingStore.cs):

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Uow;

namespace Volo.Abp.AuditLogging;

public class AuditingStore : IAuditingStore, ITransientDependency
{
    public ILogger<AuditingStore> Logger { get; set; }
    protected IAuditLogRepository AuditLogRepository { get; }
    protected IUnitOfWorkManager UnitOfWorkManager { get; }
    protected AbpAuditingOptions Options { get; }
    protected IAuditLogInfoToAuditLogConverter Converter { get; }
    public AuditingStore(
        IAuditLogRepository auditLogRepository,
        IUnitOfWorkManager unitOfWorkManager,
        IOptions<AbpAuditingOptions> options,
        IAuditLogInfoToAuditLogConverter converter)
    {
        AuditLogRepository = auditLogRepository;
        UnitOfWorkManager = unitOfWorkManager;
        Converter = converter;
        Options = options.Value;

        Logger = NullLogger<AuditingStore>.Instance;
    }

    public virtual async Task SaveAsync(AuditLogInfo auditInfo)
    {
        if (!Options.HideErrors)
        {
            await SaveLogAsync(auditInfo);
            return;
        }

        try
        {
            await SaveLogAsync(auditInfo);
        }
        catch (Exception ex)
        {
            Logger.LogWarning("Could not save the audit log object: " + Environment.NewLine + auditInfo.ToString());
            Logger.LogException(ex, LogLevel.Error);
        }
    }

    // CHANGES HERE
    protected virtual async Task SaveLogAsync(AuditLogInfo auditInfo)
    {
        using (var uow = UnitOfWorkManager.Begin(true))
        {
            var auditLog = await Converter.ConvertAsync(auditInfo);

            //Remove all EntityChanges without PropertyChanges
            auditLog.EntityChanges.RemoveAll(x => x.PropertyChanges.Count == 0);

            await AuditLogRepository.InsertAsync(auditLog);
            await uow.CompleteAsync();
        }
    }
}

Additional context

No response

developerMCSI avatar May 24 '24 09:05 developerMCSI

I think Entity changes and Entity properties changes are two different things You can continue to customize AuditingStore.

For example, if the password of a user entity changes, we should add the entity change without the password property.

maliming avatar May 27 '24 09:05 maliming

The base concept of tracking the auditing on entity or property should not be privacy, but it should be it's impact on the domain. If i want to track the auditing of certain properties within an entity, but not the entity itself, i would assume that a record would be created only when one of the tracked properties is changed.

If "Entity changes" and "Entity property changes" are two different things, as stated by you, the same logic applies, because if i added the "[DisableAuditing]" to the entity i dont want to track the entity so it won't create any "Entity changes" record. In this case the only reason for an "Entity changes" to be created is if a property, within the entity that is not tracked, that has been flagged as "[Auditing]" has changed, triggering the creation of a "Entity property change" record.

Correct?

@hikalkan thoughts on this?

P.S: Passwords are encrypted so you can still track them ;)

developerMCSI avatar May 27 '24 10:05 developerMCSI

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 26 '25 06:04 stale[bot]

After a year the ABP version has changed as well as the code, but the question remains.

RobertoFiocchiMCSI avatar Apr 27 '25 16:04 RobertoFiocchiMCSI

@maliming Why was this thread closed even though I added a post? Please, check the "stale bot" and reopen the issue. Thanks

RobertoFiocchiMCSI avatar May 06 '25 07:05 RobertoFiocchiMCSI