Disable the Audit Trail Module
- 2 minutes to read
Disable the Module Permanently
This technique allows you to stop tracking changes throughout the application. To do this, use the standard DbContextFactory instead of AuditedDbContextFactory when you create EFCoreObjectSpaceProvider<TDbContext>.
Note
With this technique, you cannot change the state of the Module during application execution.
The following example demonstrates how to declare an additional field to switch between the audited and standard DbContext factories:
ASP.NET Core Blazor
File: MySolution.Blazor.Server\BlazorApplication.cs.
using DevExpress.ExpressApp.AuditTrail.EFCore;
// ...
public partial class MySolutionBlazorApplication : BlazorApplication {
// ...
bool UseStandardDbContectFactory = true;
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
IDbContextFactory<MySolutionEFCoreDbContext> dbContextFactory =
UseStandardDbContectFactory ?
ServiceProvider.GetService<IDbContextFactory<MySolutionEFCoreDbContext>>() :
ServiceProvider.GetService<IXafDbContextFactory<MySolutionEFCoreDbContext>>();
//...
}
}
WinForms
File: MySolution.Win\WinApplication.cs.
using DevExpress.ExpressApp.AuditTrail.EFCore;
// ...
public partial class MySolutionWindowsFormsApplication : WinApplication {
// ...
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
bool UseStandardDbContectFactory = true;
IDbContextFactory<DbContext> dbContextFactory;
if(UseStandardDbContectFactory) {
dbContextFactory = new EFCoreDbContextFactory<MySolutionDbContext>(args.ConnectionString,
(builder, connectionString) => {
builder.UseSqlServer(connectionString);
// ...
});
}
else {
dbContextFactory = AuditedDbContextFactory.CreateFactory<MySolutionDbContext>(args.ConnectionString,
(builder, connectionString) => {
builder.UseSqlServer(connectionString);
// ...
});
}
//...
}
}
Disable the Module Temporarily
The AuditTrailService.Enabled property allows you to disable the Audit Trail Module for a specific scenario. You can use this technique in different parts of your application (for example, in Controllers).
The following example demonstrates this technique:
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.Enabled = false;
// non-tracked actions or changes
auditTrailService.Enabled = true;
}
}
Note
- The AuditTrailService.SaveCustomData method ignores the AuditTrailService.Enabled property. You can use this method when the property is set to false.
- If you use one Object Space in multiple places simultaneously, the Audit Trail Module does not track changes throughout the application when AuditTrailService.Enabled is set to false.