Orchard Core Audit Trail - Prompt Templates
Configure Audit Trail and Event Tracking
You are an Orchard Core expert. Generate audit trail configurations, custom event providers, and AuditTrailPart setups for Orchard Core.
Guidelines
- Enable
OrchardCore.AuditTrail for audit event recording.
- The Audit Trail provides an immutable, auditable log of system events (content changes, user events, etc.).
- Events are logged automatically for supported actions (create, publish, delete content items; user login failures, etc.).
- The audit trail list supports filtering by date range, category (Content, User, etc.), and sorting.
- Content events show version history, diffs, and allow restoring previous versions or deleted items.
- Restored content items are created as drafts that must be manually published.
- Attach
AuditTrailPart to content types to let editors add comments to audit trail entries on save.
- Configure which events to record via Settings → Audit Trail.
- Client IP address logging is optional and may require GDPR/privacy compliance considerations.
- Trimming settings control how long events are retained in the database.
- Custom modules can provide their own audit trail event handlers.
- Always seal classes.
Enabling Audit Trail
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.AuditTrail"
],
"disable": []
}
]
}
Audit Trail Event Categories
| Category |
Events |
Description |
| Content |
Created, Published, Unpublished, Removed, Cloned, Restored |
Tracks content item lifecycle events. |
| User |
LoggedIn, LogInFailed, PasswordReset, PasswordChanged |
Tracks user authentication and account events. |
Audit Trail Event List Features
| Feature |
Description |
| Date Range Filter |
Filter events to a specific time period. |
| Category Filter |
Filter by event category (e.g., Content, User). |
| Sorting |
Sort entries by various parameters (date, category, user). |
| Version Link |
Click to view the read-only editor of a content item at that version. |
| Display Text Link |
Click to edit the latest version of the content item. |
| View Button |
View the content item at the recorded version. |
| Restore Button |
Restore a content item to a previous version (creates a draft). |
| Details Link |
View detailed event information, including textual diffs for content events. |
Attaching AuditTrailPart via Migration
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;
namespace MyModule;
public sealed class AuditTrailMigrations : DataMigration
{
private readonly IContentDefinitionManager _contentDefinitionManager;
public AuditTrailMigrations(IContentDefinitionManager contentDefinitionManager)
{
_contentDefinitionManager = contentDefinitionManager;
}
public async Task<int> CreateAsync()
{
await _contentDefinitionManager.AlterTypeDefinitionAsync("{{ContentTypeName}}", type => type
.WithPart("AuditTrailPart", part => part
.WithPosition("20")
)
);
return 1;
}
}
Attaching AuditTrailPart via Recipe
{
"steps": [
{
"name": "ContentDefinition",
"ContentTypes": [
{
"Name": "{{ContentTypeName}}",
"DisplayName": "{{DisplayName}}",
"ContentTypePartDefinitionRecords": [
{
"PartName": "AuditTrailPart",
"Name": "AuditTrailPart",
"Settings": {
"ContentTypePartSettings": {
"Position": "20"
}
}
}
]
}
]
}
]
}
Audit Trail Settings
Configure at Settings → Audit Trail:
| Setting |
Description |
| Event Recording |
Enable or disable recording for specific event types. |
| Client IP Logging |
When enabled, the client IP address is included in audit trail events. Requires GDPR/privacy compliance. |
| Trimming |
Configure retention period for audit events. Disable trimming to keep events indefinitely. |
| Content Tab |
Select which content types should have their events recorded. |
Creating a Custom Audit Trail Event Provider
Implement IAuditTrailEventHandler to record custom events:
using OrchardCore.AuditTrail.Services;
using OrchardCore.AuditTrail.Services.Models;
namespace MyModule.AuditTrail;
public sealed class CustomAuditTrailEventHandler : AuditTrailEventHandlerBase
{
public override Task CreateAsync(AuditTrailCreateContext context)
{
if (context is AuditTrailCreateContext<CustomAuditEvent> customContext)
{
customContext.AuditTrailEventItem.Action = "Created";
}
return Task.CompletedTask;
}
}
public sealed class CustomAuditEvent
{
public string Action { get; set; }
}
Recording a Custom Audit Trail Event
using OrchardCore.AuditTrail.Services;
using MyModule.AuditTrail;
namespace MyModule.Services;
public sealed class MyService
{
private readonly IAuditTrailManager _auditTrailManager;
public MyService(IAuditTrailManager auditTrailManager)
{
_auditTrailManager = auditTrailManager;
}
public async Task PerformActionAsync()
{
// Perform your action...
// Record the audit trail event.
await _auditTrailManager.RecordEventAsync(
new AuditTrailContext<CustomAuditEvent>(
name: "CustomAction",
category: "MyModule",
correlationId: "{{CorrelationId}}",
userId: "{{UserId}}",
userName: "{{UserName}}",
auditTrailEventItem: new CustomAuditEvent()
)
);
}
}
Registering Custom Audit Trail Event Handler
using OrchardCore.AuditTrail.Services;
using OrchardCore.Modules;
namespace MyModule;
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IAuditTrailEventHandler, CustomAuditTrailEventHandler>();
}
}
Content Event Diff Tracking
The Audit Trail module tracks diffs between content versions:
- Current version values are shown in green.
- Previous version values are shown in red.
- Access diffs via the Details link → Diff tab in the audit trail list.
Audit Trail Trimming Configuration
{
"steps": [
{
"name": "Settings",
"AuditTrailTrimmingSettings": {
"RetentionDays": 90,
"Disabled": false
}
}
]
}
Enabling Content-Specific Audit Trail
To track specific content type events:
- Navigate to Settings → Audit Trail → Content tab.
- Select the content types to track.
- Save the settings.
Only events for selected content types will be recorded, reducing noise in the audit log.
Best Practices
- Attach
AuditTrailPart to critical content types (e.g., pages, policies) for edit comments.
- Enable IP logging only when required and ensure privacy compliance.
- Set a reasonable trimming period (e.g., 90–365 days) to manage database size.
- Use the Details/Diff view to investigate content changes before restoring.
- Custom event providers should use descriptive category and event names for easy filtering.
1---2name: orchardcore-audit-trail3description: Skill for configuring audit trail in Orchard Core. Covers audit event recording, AuditTrailPart for content tracking, event filtering and sorting, audit settings configuration, custom audit event providers, and audit trail feature setup. Use this skill when requests mention Orchard Core Audit Trail, Configure Audit Trail and Event Tracking, Enabling Audit Trail, Audit Trail Event Categories, Audit Trail Event List Features, Attaching AuditTrailPart via Migration, or closely related Orchard Core implementation, setup, extension, or troubleshooting work. Strong matches include work with OrchardCore.AuditTrail, OrchardCore.ContentManagement.Metadata, OrchardCore.Data.Migration, OrchardCore.AuditTrail.Services, OrchardCore.AuditTrail.Services.Models. It also helps with Audit Trail Event List Features, Attaching AuditTrailPart via Migration, Attaching AuditTrailPart via Recipe, plus the code patterns, admin flows, recipe steps, and referenced examples captured in this skill.4---56# Orchard Core Audit Trail - Prompt Templates78## Configure Audit Trail and Event Tracking910You are an Orchard Core expert. Generate audit trail configurations, custom event providers, and AuditTrailPart setups for Orchard Core.1112### Guidelines1314- Enable `OrchardCore.AuditTrail` for audit event recording.15- The Audit Trail provides an immutable, auditable log of system events (content changes, user events, etc.).16- Events are logged automatically for supported actions (create, publish, delete content items; user login failures, etc.).17- The audit trail list supports filtering by date range, category (Content, User, etc.), and sorting.18- Content events show version history, diffs, and allow restoring previous versions or deleted items.19- Restored content items are created as drafts that must be manually published.20- Attach `AuditTrailPart` to content types to let editors add comments to audit trail entries on save.21- Configure which events to record via **Settings** → **Audit Trail**.22- Client IP address logging is optional and may require GDPR/privacy compliance considerations.23- Trimming settings control how long events are retained in the database.24- Custom modules can provide their own audit trail event handlers.25- Always seal classes.2627### Enabling Audit Trail2829```json30{31 "steps": [32 {33 "name": "Feature",34 "enable": [35 "OrchardCore.AuditTrail"36 ],37 "disable": []38 }39 ]40}41```4243### Audit Trail Event Categories4445| Category | Events | Description |46|----------|--------|-------------|47| **Content** | Created, Published, Unpublished, Removed, Cloned, Restored | Tracks content item lifecycle events. |48| **User** | LoggedIn, LogInFailed, PasswordReset, PasswordChanged | Tracks user authentication and account events. |4950### Audit Trail Event List Features5152| Feature | Description |53|---------|-------------|54| **Date Range Filter** | Filter events to a specific time period. |55| **Category Filter** | Filter by event category (e.g., Content, User). |56| **Sorting** | Sort entries by various parameters (date, category, user). |57| **Version Link** | Click to view the read-only editor of a content item at that version. |58| **Display Text Link** | Click to edit the latest version of the content item. |59| **View Button** | View the content item at the recorded version. |60| **Restore Button** | Restore a content item to a previous version (creates a draft). |61| **Details Link** | View detailed event information, including textual diffs for content events. |6263### Attaching AuditTrailPart via Migration6465```csharp66using OrchardCore.ContentManagement.Metadata;67using OrchardCore.ContentManagement.Metadata.Settings;68using OrchardCore.Data.Migration;6970namespace MyModule;7172public sealed class AuditTrailMigrations : DataMigration73{74 private readonly IContentDefinitionManager _contentDefinitionManager;7576 public AuditTrailMigrations(IContentDefinitionManager contentDefinitionManager)77 {78 _contentDefinitionManager = contentDefinitionManager;79 }8081 public async Task<int> CreateAsync()82 {83 await _contentDefinitionManager.AlterTypeDefinitionAsync("{{ContentTypeName}}", type => type84 .WithPart("AuditTrailPart", part => part85 .WithPosition("20")86 )87 );8889 return 1;90 }91}92```9394### Attaching AuditTrailPart via Recipe9596```json97{98 "steps": [99 {100 "name": "ContentDefinition",101 "ContentTypes": [102 {103 "Name": "{{ContentTypeName}}",104 "DisplayName": "{{DisplayName}}",105 "ContentTypePartDefinitionRecords": [106 {107 "PartName": "AuditTrailPart",108 "Name": "AuditTrailPart",109 "Settings": {110 "ContentTypePartSettings": {111 "Position": "20"112 }113 }114 }115 ]116 }117 ]118 }119 ]120}121```122123### Audit Trail Settings124125Configure at **Settings** → **Audit Trail**:126127| Setting | Description |128|---------|-------------|129| **Event Recording** | Enable or disable recording for specific event types. |130| **Client IP Logging** | When enabled, the client IP address is included in audit trail events. Requires GDPR/privacy compliance. |131| **Trimming** | Configure retention period for audit events. Disable trimming to keep events indefinitely. |132| **Content Tab** | Select which content types should have their events recorded. |133134### Creating a Custom Audit Trail Event Provider135136Implement `IAuditTrailEventHandler` to record custom events:137138```csharp139using OrchardCore.AuditTrail.Services;140using OrchardCore.AuditTrail.Services.Models;141142namespace MyModule.AuditTrail;143144public sealed class CustomAuditTrailEventHandler : AuditTrailEventHandlerBase145{146 public override Task CreateAsync(AuditTrailCreateContext context)147 {148 if (context is AuditTrailCreateContext<CustomAuditEvent> customContext)149 {150 customContext.AuditTrailEventItem.Action = "Created";151 }152153 return Task.CompletedTask;154 }155}156157public sealed class CustomAuditEvent158{159 public string Action { get; set; }160}161```162163### Recording a Custom Audit Trail Event164165```csharp166using OrchardCore.AuditTrail.Services;167using MyModule.AuditTrail;168169namespace MyModule.Services;170171public sealed class MyService172{173 private readonly IAuditTrailManager _auditTrailManager;174175 public MyService(IAuditTrailManager auditTrailManager)176 {177 _auditTrailManager = auditTrailManager;178 }179180 public async Task PerformActionAsync()181 {182 // Perform your action...183184 // Record the audit trail event.185 await _auditTrailManager.RecordEventAsync(186 new AuditTrailContext<CustomAuditEvent>(187 name: "CustomAction",188 category: "MyModule",189 correlationId: "{{CorrelationId}}",190 userId: "{{UserId}}",191 userName: "{{UserName}}",192 auditTrailEventItem: new CustomAuditEvent()193 )194 );195 }196}197```198199### Registering Custom Audit Trail Event Handler200201```csharp202using OrchardCore.AuditTrail.Services;203using OrchardCore.Modules;204205namespace MyModule;206207public sealed class Startup : StartupBase208{209 public override void ConfigureServices(IServiceCollection services)210 {211 services.AddScoped<IAuditTrailEventHandler, CustomAuditTrailEventHandler>();212 }213}214```215216### Content Event Diff Tracking217218The Audit Trail module tracks diffs between content versions:219220- **Current version values** are shown in green.221- **Previous version values** are shown in red.222- Access diffs via the **Details** link → **Diff** tab in the audit trail list.223224### Audit Trail Trimming Configuration225226```json227{228 "steps": [229 {230 "name": "Settings",231 "AuditTrailTrimmingSettings": {232 "RetentionDays": 90,233 "Disabled": false234 }235 }236 ]237}238```239240### Enabling Content-Specific Audit Trail241242To track specific content type events:2432441. Navigate to **Settings** → **Audit Trail** → **Content** tab.2452. Select the content types to track.2463. Save the settings.247248Only events for selected content types will be recorded, reducing noise in the audit log.249250### Best Practices251252- Attach `AuditTrailPart` to critical content types (e.g., pages, policies) for edit comments.253- Enable IP logging only when required and ensure privacy compliance.254- Set a reasonable trimming period (e.g., 90–365 days) to manage database size.255- Use the Details/Diff view to investigate content changes before restoring.256- Custom event providers should use descriptive category and event names for easy filtering.