Skip to main content
A newer version of this page is available. .
All docs
V21.1

Miscellaneous Customizations of the Audit Trail System (EF Core)

  • 2 minutes to read

Track Changes in Objects of Specific Types

The AuditTrailOptions.AuditedTypePolicy property allows you to audit changes on all persistent objects or on a list of objects. You can set this property to one of the following AuditedTypePolicy values:

AuditAllExceptDeniedTypes (default)
The Module audits changes on all objects except objects listed in the AuditTrailOptions.DeclaredTypes collection.
AuditAllAllowedTypes
The Module audits changes on objects listed in the AuditTrailOptions.DeclaredTypes collection.

The AuditTrailOptions.DeclaredTypes collection contains the following types: ModelDifference and ModelDifferenceAspect. The example below demonstrates how to customize this collection and enable audit for CustomClass objects only:

File: MySolution.Blazor.Server\Startup.cs.

public class Startup {
    // ...
    public void ConfigureServices(IServiceCollection services) {
        // ...
        services.AddAuditTrail(options => {
            options.AuditedTypePolicy = AuditedTypePolicy.AuditAllAllowedTypes;
            options.DeclaredTypes.Clear();
            options.DeclaredTypes.Add(typeof(CustomClass));
        }).AddAuditedDbContextFactory<MySolutionEFCoreDbContext>();
    }
    // ...
}

Change the String Representation of Audited Objects

The Module stores the string representation of modified objects in the database. In the UI, the Module displays these strings in the Audit Event List View’s Audited Object column:

The following steps demonstrate how to customize the string representation of Person objects.

  1. Create the AuditDefaultStringProvider descendant, as shown below.

    using DevExpress.Persistent.BaseImpl.EF;
    using DevExpress.Persistent.BaseImpl.EFCore.AuditTrail;
    // ...
    public class MyAuditDefaultStringProvider : AuditDefaultStringProvider {
        public override string GetDefaultString(object targetObject, Type objectType, object objectKey) {
            if (objectType.Equals(typeof(Person))) {
                Person person = (Person)targetObject;
                return $"{person.FullName}";
            }
            return base.GetDefaultString(targetObject, objectType, objectKey);
        }
    }
    
  2. Register this class as a custom provider in the AddAuditTrail method (the MySolution.Blazor.Server\Startup.cs file).

    public class Startup {
        // ...
        public void ConfigureServices(IServiceCollection services) {
            // ...
            services.AddAuditTrail(options => {
                options.AuditDefaultStringProviderType = typeof(MyAuditDefaultStringProvider);
            }).AddAuditedDbContextFactory<MySolutionEFCoreDbContext>();
        }
        // ...
    }
    

The following help topic demonstrates the full example: Audit the Current User or Host Identity (EF Core).

See Also