Audit Object Changes
- 3 minutes to read
Important
The Audit Trail module is not supported by the Entity Framework ORM in the current version of XAF. If you are using EF, please skip this lesson and go to the next one.
In this lesson, you will learn how to audit and analyze the changes that are made to business objects while running the application. For this purpose, the Audit Trail module will be added to the application. Changes made to the Contact object will be audited. Two techniques to analyze them will be used.
Note
Before proceeding, take a moment to review the Inherit from the Business Class Library Class (XPO) lesson.
Audit Contact Objects
Add the AuditTrail module to your MySolution.Module project. For this purpose, find the Module.cs (Module.vb) file in the MySolution.Module project displayed in the Solution Explorer. Double-click this file to invoke the Module Designer. In the Toolbox, expand the DX.19.2: XAF Modules tab. Drag the AuditTrailModule item to the Designer’s Required Modules section.
Now, all objects that are created in the application are audited. The Audit Trail System logs information on the change type (object was created, changed, etc.), who made this change, the object that was changed, the previous and new property values, etc. When an object is saved to the database, any changes between two sequential events are registered.
Analyze Audit Log in the Application
Use the following approach to view object changes directly in the application.
Add a collection property to the Contact class. The collection’s items will provide log information retrieved from the database.
using DevExpress.ExpressApp; // ... [DefaultClassOptions] public class Contact : Person { //... private XPCollection<AuditDataItemPersistent> changeHistory; [CollectionOperationSet(AllowAdd = false, AllowRemove = false)] public XPCollection<AuditDataItemPersistent> ChangeHistory { get { if(changeHistory == null) { changeHistory = AuditedObjectWeakReference.GetAuditTrail(Session, this); } return changeHistory; } } }
Run the WinForms or ASP.NET application and invoke a Contact Detail View. Modify the Contact object to test the auditing capability, save the changes and click Refresh (). The Change History collection will contain information on the previous Contact object changes.
Note
As you may remember, the Office property is declared in the Department class, not the Contact class. So, changes to the Office property made using the Contact Detail View are not displayed in the Contact‘s Change History. Instead, these changes appear in the corresponding Department object’s Change History (if changes made to the Department objects are audited). You can acquire the audit log remotely using SQL queries to your database. See the Analyze the Audit Log topic.
You can view the code demonstrated here in the Contact.cs (Contact.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version of this demo is available online at https://demos.devexpress.com/XAF/MainDemo.
Next Lesson: Highlight List View Objects