Customize the Data Storing Mechanism
- 3 minutes to read
The Audit Trail Module stores audit records (AuditDataItem
objects) in an application database. The SaveAuditTrailData event allows you to configure how this Module saves information. This event is raised when the Module commits an AuditDataItem
object to the database. In this event handler, you can modify the SaveAuditTrailDataContext.AuditTrailDataItems
(in .NET applications) or SaveAuditTrailDataEventArgs.AuditTrailDataItems
(in .NET Framework applications) collection, and add new or remove unnecessary items. The following code demonstrates how to remove ObjectCreated
records.
In .NET Applications
File: MySolution.Blazor.Server\Startup.cs, MySolution.Win\Startup.cs, MySolution.WebApi\Startup.cs
using DevExpress.Persistent.AuditTrail;
// ...
builder.Modules
.AddAuditTrailXpo(o => {
o.Events.OnCustomizeAuditOperationTypeFilter = context => {
if (context.AuditOperationType == AuditOperationType.ObjectChanged ||
context.AuditOperationType == AuditOperationType.ObjectCreated) {
context.SaveAuditOperation = false;
}
};
})
In .NET Framework Applications
File: MySolution.Win\Program.cs, MySolution.Web\Global.asax.cs
using DevExpress.Persistent.AuditTrail;
// ...
static class Program {
// ...
static void Main() {
MySolutionWindowsFormsApplication application = new MySolutionWindowsFormsApplication();
// ...
AuditTrailService.Instance.SaveAuditTrailData +=
new SaveAuditTrailDataEventHandler(Instance_SaveAuditTrailData);
application.Setup();
application.Start();
// ...
}
static void Instance_SaveAuditTrailData(object sender, SaveAuditTrailDataEventArgs e) {
for(int i = e.AuditTrailDataItems.Count - 1; i >= 0; i--) {
var item = e.AuditTrailDataItems[i];
if(item.OperationType == AuditOperationType.ObjectCreated) {
e.AuditTrailDataItems.RemoveAt(i);
}
}
}
}
Replace the Default Data Storing Mechanism with a Custom Mechanism
To do this, set the event’s SaveAuditTrailDataContext.Handled
(in .NET applications) or SaveAuditTrailDataEventArgs.Handled
(in .NET Framework applications) argument to true
in the SaveAuditTrailData
event handler.
In .NET Applications
File: MySolution.Blazor.Server\Startup.cs, MySolution.Win\Startup.cs, MySolution.WebApi\Startup.cs
using DevExpress.Persistent.AuditTrail;
// ...
builder.Modules
.AddAuditTrailXpo(o => {
o.Events.OnCustomizeAuditOperationTypeFilter = context => {
// Save the data passed as the `context.AuditTrailDataItems` parameter
context.Handled = true; // Disable the default data storing mechanism
};
})
In .NET Framework Applications
File: MySolution.Win\Program.cs, MySolution.Web\Global.asax.cs
using DevExpress.Persistent.AuditTrail;
// ...
static class Program {
// ...
static void Main() {
MySolutionWindowsFormsApplication application = new MySolutionWindowsFormsApplication();
// ...
AuditTrailService.Instance.SaveAuditTrailData +=
new SaveAuditTrailDataEventHandler(Instance_SaveAuditTrailData);
application.Setup();
application.Start();
// ...
}
static void Instance_SaveAuditTrailData(object sender, SaveAuditTrailDataEventArgs e) {
// Save the data passed as the `e.AuditTrailDataItems` parameter
e.Handled = true; // Disable the default data storing mechanism
}
}
Alternative Techniques
- If you do not want to log specific operations, implement the technique described in the Audit Specific Operations topic.
- You can create the
AuditDataStore
descendant and override itsSave
method. Register this custom data store within the Audit Trail system as described in the following topic: Miscellaneous Customizations of the Audit Trail System (XPO).