Specify Objects and Properties to Be Audited
- 4 minutes to read
The Audit Trail Module logs changes in the following objects and properties:
- Persistent classes.
- Public writable simple and reference properties defined in persistent classes.
- Public collection properties defined in persistent classes.
Read-only properties (those without a setter) and properties decorated with the NonPersistentAttribute are excluded from the audit trail.
You can modify default audit settings — specify objects and properties to be audited or excluded from the audit process.
Specify Audit Settings Globally
You can handle the CustomizeAuditTrailSettings event or apply the UseInAuditTrailAttribute to add or remove objects and properties in/from audit globally.
Handle CustomizeAuditTrailSettings Event
Subscribe to the CustomizeAuditTrailSettings event in the AddAuditTrailXpo method call to modify audit settings:
using DevExpress.Persistent.AuditTrail;
// ...
builder.Modules
.AddAuditTrailXpo(o => {
o.Events.OnCustomizeAuditTrailSettings = context => {
// Clear the default settings:
context.AuditTrailSettings.Clear();
// Add a type's specific properties:
context.AuditTrailSettings.AddType(typeof(MyObjectType1), "FirstPropertyToBeAudited", "SecondPropertyToBeAudited");
// Add all properties of the type:
context.AuditTrailSettings.AddType(typeof(MyObjectType2));
// Exclude the type's properties:
context.AuditTrailSettings.RemoveProperties(typeof(MyObjectType2), "PropertyToBeExcluded");
};
})
Apply UseInAuditTrail Attribute
Use the UseInAuditTrailAttribute to specify whether a property takes part in audit.
// remove the persistent property from audit
[UseInAuditTrail(false)]
public string MyPersistentProperty {
get { return myPersistentProperty; }
set { SetPropertyValue(nameof(MyPersistentProperty), ref myPersistentProperty, value); }
}
// add the non-persistent property in audit
[UseInAuditTrail(true)]
[NonPersistent]
public string MyNonPersistentProperty {
get { return myNonPersistentProperty; }
set { SetPropertyValue(nameof(MyNonPersistentProperty), ref myNonPersistentProperty, value); }
}
Specify Audit Settings for the Current Scope
Use the service provider’s GetRequiredService method to access audit trail options and modify audit settings in the current scope only. The following code sample demonstrates how to change audit settings based on the current user:
using DevExpress.ExpressApp.AuditTrail;
using DevExpress.ExpressApp.Security;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
public class Startup {
// ...
public void ConfigureServices(IServiceCollection services) {
// ...
services.AddScoped<CircuitHandler, CircuitHandlerProxy>();
services.AddXaf(Configuration, builder => {
builder.Security
.UseIntegratedMode(options => {
options.Events.OnLogon += context => {
string userName = ((ApplicationUser)context.User).UserName;
if (userName == "User") {
var auditOptions = context.ServiceProvider.GetRequiredService<IOptionsSnapshot<AuditTrailOptions>>().Value;
auditOptions.Events.OnCustomizeAuditTrailSettings = context => {
// Clear the default settings:
context.AuditTrailSettings.Clear();
// Add a type's specific properties:
context.AuditTrailSettings.AddType(typeof(MyObjectType1), "FirstPropertyToBeAudited", "SecondPropertyToBeAudited");
// Add all properties of the type:
context.AuditTrailSettings.AddType(typeof(MyObjectType2));
// Exclude the type's properties:
context.AuditTrailSettings.RemoveProperties(typeof(MyObjectType2), "PropertyToBeExcluded");
};
}
};
//...
})
// ...
}
// ...
}
// ...
}
Note that the AuditTrailOptions type is wrapped into IOptionsSnapshot, which is then passed as the GetRequiredService method’s type parameter. This is done to adjust the Audit Trail module settings in the current scope only. For more information, refer to the following topic: Use IOptionsSnapshot to read updated data.
Handle CustomizeAuditTrailSettings Event in .NET Framework Applications
To customize audit settings in a .NET Framework Application, handle the AuditTrailService.Instance.CustomizeAuditTrailSettings event in the following way:
using DevExpress.Persistent.AuditTrail;
//...
static void Main() {
MySolutionWindowsFormsApplication application = new MySolutionWindowsFormsApplication();
// ...
AuditTrailService.Instance.CustomizeAuditTrailSettings +=
new CustomizeAuditSettingsEventHandler(Instance_CustomizeAuditTrailSettings);
application.Setup();
application.Start();
//...
}
static void Instance_CustomizeAuditTrailSettings(object sender,
CustomizeAuditTrailSettingsEventArgs e) {
// Clear the default settings:
e.AuditTrailSettings.Clear();
// Add a type's specific properties:
e.AuditTrailSettings.AddType(typeof(MyObjectType1), "FirstPropertyToBeAudited", "SecondPropertyToBeAudited");
// Add all properties of the type:
e.AuditTrailSettings.AddType(typeof(MyObjectType2));
// Exclude the type's properties:
e.AuditTrailSettings.RemoveProperties(typeof(MyObjectType2), "PropertyToBeExcluded");
}