Skip to main content

Audit Specific Operations

  • 3 minutes to read

You can instruct Audit Trail Module not to log specific operations. For example, the following code samples demonstrate how to exclude the ObjectChanged and ObjectCreated records from the audit log.

In .NET Applications

Handle the AuditTrailEvents.OnCustomizeAuditOperationTypeFilter event, check the current operation type, and set the SaveAuditOperation argument to false.

Exclude Operations Globally

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

using DevExpress.Persistent.AuditTrail;
// ...
builder.Modules
    .AddAuditTrailXpo(o => {
        o.Events.OnCustomizeAuditOperationTypeFilter = context => {
            if (context.AuditOperationType == AuditOperationType.ObjectChanged ||
                context.AuditOperationType == AuditOperationType.ObjectCreated) {
                context.SaveAuditOperation = false;
            }
        };
    })

Exclude Operations in the Current Scope

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.OnCustomizeAuditOperationTypeFilter = context => {
                                if (context.AuditOperationType == AuditOperationType.ObjectChanged ||
                                    context.AuditOperationType == AuditOperationType.ObjectCreated) {
                                    context.SaveAuditOperation = false;
                                }
                            };
                        }
                    };
                    //...
                })
            // ...
        }
        // ...
    }
    // ...
}

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

In .NET Framework applications, you need to handle the CustomizeAuditOperationTypeFilter event in a similar way to what is described for .NET:

Cross-Platform Module

File: MySolution.Module\MySolutionModule.cs

using DevExpress.Persistent.AuditTrail;
using static DevExpress.Persistent.AuditTrail.AuditTrailService;
// ...
public sealed partial class MySolutionModule : ModuleBase { 
    public override void Setup(XafApplication application) {
        base.Setup(application); 
        AuditTrailService.Instance.CustomizeAuditOperationTypeFilter += 
            new EventHandler<CustomAuditOperationTypeFilterEventArgs>(Instance_CustomizeAuditOperationTypeFilter);  
        //...  
    }  
    private static void Instance_CustomizeAuditOperationTypeFilter(object sender, CustomAuditOperationTypeFilterEventArgs e) {  
        if(e.AuditOperationType == AuditOperationType.ObjectChanged || 
            e.AuditOperationType == AuditOperationType.ObjectCreated) {
            e.SaveAuditOperation = false;
        }
    }
    // ...
}  

WinForms

File: MySolution.Win\Program.cs

using DevExpress.Persistent.AuditTrail;
using static DevExpress.Persistent.AuditTrail.AuditTrailService;
// ...
public class Program {  
    public static void Main() {  
        //...  
        MySolutionWinApplication winApplication = new MySolutionWinApplication();  
        AuditTrailService.Instance.CustomizeAuditOperationTypeFilter += 
            new EventHandler<CustomAuditOperationTypeFilterEventArgs>(Instance_CustomizeAuditOperationTypeFilter);  
        //...  
    }  
    private static void Instance_CustomizeAuditOperationTypeFilter(object sender, CustomAuditOperationTypeFilterEventArgs e) {  
        if(e.AuditOperationType == AuditOperationType.ObjectChanged || 
            e.AuditOperationType == AuditOperationType.ObjectCreated) {
            e.SaveAuditOperation = false;
        }
    }
    // ...
}  

ASP.NET Web Forms

File: MySolution.Web\Global.asax.cs

using DevExpress.Persistent.AuditTrail;
using static DevExpress.Persistent.AuditTrail.AuditTrailService;
// ...
public class Global : System.Web.HttpApplication {
    protected void Session_Start(object sender, EventArgs e) {
        //...  
        WebApplication.SetInstance(Session, new MySolutionWebApplication());  
        AuditTrailService.Instance.CustomizeAuditOperationTypeFilter += 
            new EventHandler<CustomAuditOperationTypeFilterEventArgs>(Instance_CustomizeAuditOperationTypeFilter);  
        //...  
    }  
    private static void Instance_CustomizeAuditOperationTypeFilter(object sender, CustomAuditOperationTypeFilterEventArgs e) {  
        if(e.AuditOperationType == AuditOperationType.ObjectChanged || 
            e.AuditOperationType == AuditOperationType.ObjectCreated) {
            e.SaveAuditOperation = false;
        }
    }
    // ...
}