Skip to main content

Add Custom Data to the Audit Log

Object changes are represented by AuditDataItem objects (see Audit Trail Module Overview). You can add custom data (information on events that are not audited) to the audit log directly. This means that you can add an AuditDataItem object to the Audit Data Items collection. For this purpose, specify the following code.

using DevExpress.ExpressApp;
using DevExpress.Persistent.AuditTrail;
using DevExpress.Xpo;
using Microsoft.Extensions.DependencyInjection;
// ...
var auditTrailService = serviceProvider.GetRequiredService<AuditTrailService>();
AuditDataItem customDataItem = new AuditDataItem(myAuditedObject, null, 
    "CustomOldValue", "CustomNewValue", AuditOperationType.CustomData);
auditTrailService.AddCustomAuditData(mySession, customDataItem);
auditTrailService.SaveAuditData(mySession);

This code can be implemented at any point in a program. For instance, in a custom Controller.

Note

You should always invoke the SaveAuditData method after adding custom audit data manually, without reusing the Session of the View’s ObjectSpace. Otherwise, the audit data will not be written to the database.