Skip to content

Commit

Permalink
Merge pull request #18715 from abpframework/SaveEntityHistoryWhenNavi…
Browse files Browse the repository at this point in the history
…gationChanges

Add `SaveEntityHistoryWhenNavigationChanges` to `AbpAuditingOptions `.
  • Loading branch information
realLiangshiwei authored Jan 8, 2024
2 parents 6ea8f1d + 155b744 commit 2f70082
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/en/Audit-Logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Here, a list of the options you can configure:
* `ApplicationName`: If multiple applications are saving audit logs into a single database, set this property to your application name, so you can distinguish the logs of different applications. If you don't set, it will set from the `IApplicationInfoAccessor.ApplicationName` value, which is the entry assembly name by default.
* `IgnoredTypes`: A list of `Type`s to be ignored for audit logging. If this is an entity type, changes for this type of entities will not be saved. This list is also used while serializing the action parameters.
* `EntityHistorySelectors`: A list of selectors those are used to determine if an entity type is selected for saving the entity change. See the section below for details.
* `SaveEntityHistoryWhenNavigationChanges` (default: `true`): If you set to true, it will save entity changes to audit log when any navigation property changes.
* `Contributors`: A list of `AuditLogContributor` implementations. A contributor is a way of extending the audit log system. See the "Audit Log Contributors" section below.
* `AlwaysLogSelectors`: A list of selectors to save the audit logs for the matched criteria.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public class AbpAuditingOptions

public IEntityHistorySelectorList EntityHistorySelectors { get; }

/// <summary>
/// Default: true.
/// Save entity changes to audit log when any navigation property changes.
/// </summary>
public bool SaveEntityHistoryWhenNavigationChanges { get; set; } = true;

//TODO: Move this to asp.net core layer or convert it to a more dynamic strategy?
/// <summary>
/// Default: false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public virtual List<EntityChangeInfo> CreateChangeList(ICollection<EntityEntry>
case EntityState.Modified:
changeType = IsDeleted(entityEntry) ? EntityChangeType.Deleted : EntityChangeType.Updated;
break;
case EntityState.Unchanged:
case EntityState.Unchanged when Options.SaveEntityHistoryWhenNavigationChanges:
changeType = EntityChangeType.Updated; // Navigation property changes.
break;
case EntityState.Detached:
Expand Down Expand Up @@ -186,7 +186,7 @@ protected virtual List<EntityPropertyChangeInfo> GetPropertyChanges(EntityEntry
}
}

if (entityEntry.State == EntityState.Unchanged)
if (Options.SaveEntityHistoryWhenNavigationChanges && entityEntry.State == EntityState.Unchanged)
{
foreach (var navigation in entityEntry.Navigations)
{
Expand Down Expand Up @@ -227,7 +227,7 @@ protected virtual bool ShouldSaveEntityHistory(EntityEntry entityEntry, bool def
return false;
}

if (entityEntry.State == EntityState.Unchanged)
if (Options.SaveEntityHistoryWhenNavigationChanges && entityEntry.State == EntityState.Unchanged)
{
if (entityEntry.Navigations.Any(navigationEntry => navigationEntry.IsModified))
{
Expand Down
Loading

0 comments on commit 2f70082

Please sign in to comment.