Files to look at:
The XAF Audit Trail module is intended to track changes being made in persistent objects. This module uses the AuditTrailService class to track changes and write them to the database. It is possible to use this API in a non-XAF application based on XPO, without creating and configuring the DevExpress.ExpressApp > XafApplication object as it is usually done in XAF apps.
To use these features of the Audit Trial module, you will need to reference the DevExpress.Persistent.BaseImpl and DevExpress.Persistent.Base assemblies, which are part of the standard XAF delivery package.
Important notes:
1. You need to have a valid license for the eXpressApp Framework to use this example.
2. AuditTrailService cannot be used with ExplicitUnitOfWork. Use UnitOfWork or Session classes to audit data through AuditTrailService. AuditTrailService creates a new session to save audit records based on the data layer of the audited session. So, the data layer becomes shared among this new temporary session and ExplicitUnitOfWork. This violates the "exclusive owner of the database connection" requirement of ExplicitUnitOfWork.
See Also:
Audit Trail Module Overview
How to reuse XAF Audit Trail module functionality in a non-XAF ASP.NET application
How to track changes made to persistent objects, and write them into a separate table
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.Persistent.AuditTrail;
using System.Reflection;
using System.Security.Principal;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
namespace WinExample {
public partial class Form1 : Form {
public Form1() {
//XpoDefault.DataLayer = XpoDefault.GetDataLayer("XpoProvider=InMemoryDataStore", DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema);
XpoDefault.DataLayer = XpoDefault.GetDataLayer(@"Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\mssqllocaldb;Initial Catalog=myAudit", DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema);
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
AuditTrailService.GetService(null).QueryCurrentUserName += new QueryCurrentUserNameEventHandler(Instance_QueryCurrentUserName);
session1.Dictionary.GetDataStoreSchema(typeof(PersistentObject1).Assembly);
AuditTrailService.GetService(null).SetXPDictionary(session1.Dictionary);
AuditTrailService.GetService(null).AuditDataStore = new AuditDataStore<AuditDataItemPersistent, AuditedObjectWeakReference>();
AuditTrailService.GetService(null).BeginSessionAudit(session1, AuditTrailStrategy.OnObjectChanged, ObjectAuditingMode.Full);
}
void Instance_QueryCurrentUserName(object sender, QueryCurrentUserNameEventArgs e) {
e.CurrentUserName = WindowsIdentity.GetCurrent().Name;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
AuditTrailService.GetService(null).SaveAuditData(session1);
}
}
}