Skip to main content

Specify the Objects and Properties to Be Audited

  • 3 minutes to read

You can modify default audit settings, and specify objects and properties to be audited or excluded from the audit process. For this purpose, subscribe to the CustomizeAuditTrailSettings event. The following code samples demonstrate how to handle this event in .NET and .NET Framework applications.

In .NET Applications

Specify Objects and Properties Globally

In the application’s Startup.cs file, add the following code to the AddAuditTrailXpo method call to modify audit settings:

File: MySolution.Blazor.Server\Startup.cs, MySolution.Win\Startup.cs, MySolution.WebApi\Startup.cs

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");
        };
    })

Specify Objects and Properties 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:

File: MySolution.Blazor.Server/Startup.cs, MySolution.Win/Startup.cs

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.

In .NET Framework Applications

To customize audit settings in a .NET Framework Application, handle the AuditTrailService.Instance.CustomizeAuditTrailSettings event in the following way:

File: MySolution.Win\Program.cs, MySolution.Web\Global.asax.cs

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");
}