Audit

Orchard Core has built-in features that can help with auditing and tracking changes, particularly for content and users.

To fully utilize auditing for both user-related actions and other general activities, we need to enable both OrchardCore.AuditTrail and OrchardCore.Users.AuditTrail.

These two Audit Trail features need to be enabled to ensure comprehensive auditing.

Configure Audit Trail

After the audit trail features are enabled, we need to configure it by specifying which events need to be recorded.

Enabling Product Information content events to be recorded.

Orchard Core supports audit trimming, which allows us to specify a retention period for audit events. This means we can configure how long audit data should be kept before it is automatically deleted or "trimmed." This helps keep the audit trail database from growing too large over time, especially in high-traffic applications.

The default retention period is 10 days.

Audit Trail module supports tracking various events for Content Items. These events provide a comprehensive audit trail of changes and actions performed on content, which is useful for tracking modifications, maintaining an activity log, and ensuring compliance.

At the end of the list, we can enable the recording of client IP address.

About Audit Trail

After we have configured the logging of Product Information, when we perform any of the enabled events, there should be a record logged.

We can view the logs under the Audit Trail section in the admin dashboard. The logs will be categorised, making it easy to distinguish between general activities and user-related actions.

This shows the log after we publish one of the Product Information content items.

Clicking on the View button or directly on the version, i.e. Version 1, it will display the content of the Product Information at that version.

The content of Product Information at the moment of logging will be kept.

Clicking on the Details button, we can view the detail of the Audit log.

Details of the audit trail.

Orchard Core also offers a page showing the line-by-line differences between two changes with its "Diff View". It compares changes between the two audit events.

In the Diff tab, we will be able to see which part of the content has been edited.

Record Event Programatically

Let's assume now we are recording a new event for our Product Information, we will first need to retrieve the corresponding content item with its contentItemId.

var productInformation = await orchard.GetContentItemByIdAsync(contentItemId);

Then, we can use the IAuditTrailManager service to record the event, as shown below.

await auditTrailManager.RecordEventAsync(
    new AuditTrailContext<AuditTrailContentEvent>
    (
        name: "Published",
        category: "Content",
        correlationId: Guid.NewGuid().ToString(),
        userId: "4gyfgah3k3ness5adpmffgntnc",
        userName: "ADMIN",
        auditTrailEventItem: new AuditTrailContentEvent
        {
            ContentItem = productInformation,
            VersionNumber = 1,
            Comment = $"This is created at {DateTime.Now:yyyy-MM-dd HH:mm:ss}."
        }
    )
);

What is shown above is just a sample. The name can only be one of the enabled Events, such as Created, Saved, Published, etc. Otherwise the audit log will not be saved to the database.

The category must be "Content" since we are recording audit log for a content item. For user audit, it will be "User".

A correlationIdrefers to a unique identifier used to group or track related events across in our CMS. It serves as a common reference point to associate events that are part of the same workflow or transaction, making it easier to trace and diagnose issues.

The userId and userName in the sample above refer to the demo Admin account for demo purpose. You should change it to use the user ID and user name of the current user.

For the VersionNumber, if the Versionable is not enabled in the option of the content type, it should always be 1. Otherwise, we can use the following to retrieve the current version of a content item.

int version = (await contentManager.GetAllVersionsAsync(contentItemId)).Count();

The code basically will just count the number of entries in the database with the same contentItemId, as demonstrated below.

The current version of content item with the ID "4fdpjjm5cgqz3y5zzaz72ccp4a" is 22 now.

Last updated