Skip to main content
All docs
V23.2

Miscellaneous Customizations of the Audit Trail System (EF Core)

  • 5 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:

ASP.NET Core Blazor

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>();
    }
    // ...
}

WinForms

File: MySolution.Win\WinApplication.cs.

using DevExpress.Persistent.BaseImpl.EFCore.AuditTrail;
    //...
    public class MySolutionWindowsFormsApplication : WinApplication {
        // ...
        protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
            var dbFactory = AuditedDbContextFactory.CreateFactory<MySolutionEFCoreDbContext>(args.ConnectionString,
                (builder, connectionString) => {
                    builder.UseSqlServer(connectionString);
                },
            (options) => {
                options.AuditedTypePolicy = AuditedTypePolicy.AuditAllAllowedTypes;
                options.DeclaredTypes.Clear();
                options.DeclaredTypes.Add(typeof(CustomClass));
            });
        }
    }

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 Audited Object column of the Audit Event View:

ASP.NET Core Blazor

Change the String Representation of Audited Objects

WinForms

Change the String Representation of Audited Objects

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 the MyAuditDefaultStringProvider 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>();
        }
        // ...
    }
    

    In WinForms applications, create an instance of the MyAuditDefaultStringProvider class and set the AuditDefaultStringProvider property to this instance:

    File: MySolution.Win\WinApplication.cs.

    using DevExpress.Persistent.BaseImpl.EFCore.AuditTrail;
    //...
    public class MySolutionWindowsFormsApplication : WinApplication {
        // ...
        protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
            var dbFactory = AuditedDbContextFactory.CreateFactory<MySolutionEFCoreDbContext>(args.ConnectionString,
                (builder, connectionString) => {
                    builder.UseSqlServer(connectionString);
                },
            (options) => {
                options.AuditDefaultStringProvider = new MyAuditDefaultStringProvider();
            });
        }
    }
    

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

Save Specific Audit Records

This section demonstrates how to customize the Audit Trail mechanism to prevent the storage of audit records with specific information such as the following:

  • Information on modifications of properties with “Password” in their names
  • Information on object deletions

Follow the steps below to implement this customization:

  1. Create a custom CustomAuditFilterDataProvider class that implements the IAuditFilterDataProvider interface.
  2. In the NeedToSave method, return true for records that the Audit Module saves to the database:

    File: MySolution.Module\CustomAuditFilterDataProvider.cs.

    using DevExpress.Persistent.BaseImpl.EFCore.AuditTrail;
    //...
    public class CustomAuditFilterDataProvider : IAuditFilterDataProvider {
        public bool NeedToSave(IAuditDataItemPersistent auditDataItemPersistent) {
            if(auditDataItemPersistent.PropertyName?.Contains("Password") == true 
                || auditDataItemPersistent.OperationType == "ObjectDeleted") {
                return false;
            }
            return true;
        }
    }
    

    The following help topic lists all operation types: Tracked Changes.

  3. Register the CustomAuditFilterDataProvider class in the IServiceCollection.AddAuditTrail (ASP.NET Core Blazor) or AuditedDbContextFactory.CreateFactory (WinForms) methods:

    ASP.NET Core Blazor
    File: MySolution.Blazor.Server\Startup.cs.

    using DevExpress.Persistent.BaseImpl.EFCore.AuditTrail;
    //...
    public void ConfigureServices(IServiceCollection services) {
        //...
        services.AddAuditTrail(options => {
            options.AuditFilterDataProviderType = typeof(CustomAuditFilterDataProvider);
        });
    }
    

    WinForms
    File: MySolution.Win\WinApplication.cs.

    using DevExpress.Persistent.BaseImpl.EFCore.AuditTrail;
    //...
    public partial class MySolutionWindowsFormsApplication : WinApplication {
        // ...
        protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
            var dbContextFactory = AuditedDbContextFactory.CreateFactory<MySolutionEFCoreDbContext>(
                // ...
                (options) => {
                    // ...
                    options.AuditFilterDataProvider = new CustomAuditFilterDataProvider();
                });
            // ...
        }
    }
    

Note

Alternatively, you can inherit the AuditFilterDataProvider class and override its NeedToSave virtual method instead of implementing the IAuditFilterDataProvider interface.

Add Custom Data to the Audit Log

The Audit Trail Module also allows you to track user activity not related to persistent object changes. For example, you can save information on the use of Actions in your application.

The following example demonstrates how to save an audit record when a user executes the CustomController.CustomAction SimpleAction:

  1. In the Action’s Execute event handler, use one of the following extension methods to get an instance of AuditTrailService:

    • EFCoreObjectSpace.GetAuditTrailService
    • DbContext.AuditTrailService
  2. In the AuditTrailService.SaveCustomData method’s delegate parameter, specify custom information that the Audit Trail Module should save. This delegate has the following parameters:

    • The data parameter that is an IAuditDataItemPersistent object. The Audit Trail Module saves this object in the database. You can customize this object before the Module saves it.
    • The getWeakReference parameter that is a delegate. You can use it to convert a persistent object to an IEFCoreWeakReference object. The Module can save this converted object as AuditedObject.

    Note

    AuditTrailService exposes the UserName and ModifiedOn data automatically.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.EFCore;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EFCore.AuditTrail;
// ...
public class CustomController : ViewController<DetailView> {
    public CustomController() {
        SimpleAction customAction = new SimpleAction(this, "CustomAction", PredefinedCategory.View);
        customAction.Execute += CustomAction_Execute;
    }
    private void CustomAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
        // ...
        AuditTrailService auditTrailService = ((EFCoreObjectSpace)ObjectSpace).GetAuditTrailService();
        auditTrailService.SaveCustomData((data, getWeakReference) => {
            data.Description = "CustomAction is executed.";
            data.AuditedObject = getWeakReference(View.CurrentObject);
        });
    }
}
See Also