Audit Information¶
Overview¶
The Audit Information feature tracks the creation and last modification details of entities automatically. It records:
- Who created or updated the entity.
- When the action occurred.
To use this feature, entities must implement one of the following interfaces based on their requirements:
IAuditInfo
: For entities requiring both creation and modification audit information.ICreatedAuditInfo
: For entities requiring only creation audit information.IUpdatedAuditInfo
: For entities requiring only modification audit information.
Messages that need to track audit information implement either the
ICreatedAuditInfoMessage
or IUpdatedAuditInfoMessage
interfaces.
Core Features¶
- Creation Information: Tracks the user and timestamp of entity creation.
- Modification Information: Tracks the user and timestamp of the last modification.
- Automatic Population: Eliminates manual input through interceptors.
- Consistency: Ensures all entities with
IAuditInfo
,ICreatedAuditInfo
, orIUpdatedAuditInfo
are audited uniformly.
How It Works¶
Audit tracking is handled via different interceptors and decorators depending on the interface implemented.
Entity Audit Tracking¶
-
IAuditInfo
Tracking:The
AuditInfoRepositoryInterceptor<TEntity>
interceptor updates audit fields automatically during create and update operations. TheAuditInfoDecorator<TEntity>
configures the necessary properties in the Entity Framework context.-
On Creation:
CreatedBy
→ Current user's ID.CreatedAt
→ Current timestamp.LastUpdatedBy
→ Current user's ID.LastUpdated
→ Current timestamp.
-
On Update:
LastUpdatedBy
→ Current user's ID.LastUpdated
→ Current timestamp.
-
-
ICreatedAuditInfo
Tracking:The
CreatedAuditInfoRepositoryInterceptor<TEntity>
interceptor updates audit fields automatically during create operations. TheCreatedAuditInfoDecorator<TEntity>
configures the necessary properties in the Entity Framework context.- On Creation:
CreatedBy
→ Current user's ID.CreatedAt
→ Current timestamp.
- On Creation:
-
IUpdatedAuditInfo
Tracking:The
UpdatedAuditInfoRepositoryInterceptor<TEntity>
interceptor updates audit fields automatically during create and update operations. TheUpdatedAuditInfoDecorator<TEntity>
configures the necessary properties in the Entity Framework context.- On Creation:
LastUpdatedBy
→ Current user's ID.LastUpdated
→ Current timestamp.
- On Update:
LastUpdatedBy
→ Current user's ID.LastUpdated
→ Current timestamp.
- On Creation:
Message Audit Tracking¶
-
Message Creation Information (
ICreatedAuditInfoMessage
):Messages implementing the
ICreatedAuditInfoMessage
interface can also track the creation details. This is useful for replicating messages. TheCreatedBy
andCreatedAt
properties must be populated by the process responsible for creating the message (usually the consumer). -
Message Update Information (
IUpdatedAuditInfoMessage
):Messages implementing the
IUpdatedAuditInfoMessage
interface must have theirLastUpdatedBy
andLastUpdated
properties populated by the application code. This is typically done by the process responsible for updating the message.
The interceptors uses ISuiteContext
to fetch the current user's ID. If no user
ID is available, an exception is thrown.
Metadata Entity Tracking¶
-
IUpdatedAuditInfoMetadata
Tracking:Entities implementing the
IUpdatedAuditInfoMetadata
interface must have theirLastUpdatedBy
andLastUpdated
properties populated by the application code. This is typically done in the service or application layer when updating the metadata entity. For example:
Default Behavior¶
When depending on EntityFrameworkCoreModule, audit tracking is automatically applied based on the interface implemented by the entity:
IAuditInfo
Entities: Full audit tracking is automatically enabled usingAuditInfoRepositoryInterceptor<TEntity>
andAuditInfoDecorator<TEntity>
.ICreatedAuditInfo
Entities: Creation-only tracking is automatically enabled usingCreatedAuditInfoRepositoryInterceptor<TEntity>
andCreatedAuditInfoDecorator<TEntity>
.IUpdatedAuditInfo
Entities: Update-only tracking is automatically enabled usingUpdatedAuditInfoRepositoryInterceptor<TEntity>
andUpdatedAuditInfoDecorator<TEntity>
. No additional setup is required to enable these features.
Database Migrations¶
A database migration is necessary for entities that implement IAuditInfo
,
ICreatedAuditInfo
, IUpdatedAuditInfo
, or IUpdatedAuditInfoMetadata
to have
the new audit-related fields (e.g., CreatedBy, CreatedAt, LastUpdated,
LastUpdatedBy) added to the database schema. Be sure to create migrations after
adding the necessary interfaces to your entities.
For entities that implement IUpdatedAuditInfoMetadata
, ensure that migrations
are applied in all contexts where these metadata entities are used.