Skip to main content
A newer version of this page is available.

Server-Side Timestamping in the Audit Trail System

  • 4 minutes to read

By default, the Audit Trail system uses the client time stamping. When an Audit Data Item is saved to the data storage, the ModifyOn property is initialized with the time of the computer which has been used to perform the change. This can cause information inauthenticity, because two changes made at the same time can be saved as if they were made at different moments (for example, if these computers are in the different time zones). To avoid this effect, you should use server-side time stamping. To do this, you need to replace the default Local Audit Timestamp Strategy with a server side time stamp strategy. This topic demonstrates how to implement a custom Audit Timestamp Strategy, and set it for the Audit Trail system.

To implement an Audit Timestamp Strategy, you should implement the IAuditTimestampStrategy interface. Further on, you will see how to implement a Timestamp Web Service and MSSql Server Timestamp Strategy.

Tip

A complete sample project is available in the DevExpress Code Examples database at Server-Side Timestamping in the Audit Trail System.

Timestamp Web Service

  • Add an Asp.NET Web Service to your solution.
  • Add the GetTime method to the new service’s implementation:

    [WebService(Namespace = "http://localhost/TimestampService/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class TimestampWebService : System.Web.Services.WebService {
       [WebMethod]
       public DateTime GetTime() {
          return DateTime.Now;
       }
    }
    
  • For the WebService attribute, specify the required URL (see the code above).
  • Set a Web reference to the new Web Service project to the application module.
  • In the application module, implement a WebServiceTimestampStrategy class. It is demonstrated in the following code:

    using System;
    using AuditServerSideTimestampingDemo.Module.TimestampService;
    using DevExpress.Persistent.AuditTrail;
    using DevExpress.Xpo;
    namespace MySolution.Module {
       public class WebServiceTimestampStrategy : IAuditTimestampStrategy {
          DateTime cachedTimestamp;
          #region IAuditTimestampStrategy Members
          public DateTime GetTimestamp(AuditDataItem auditDataItem) {
             return cachedTimestamp;
          }
          public void OnBeginSaveTransaction(Session session) {
             try {
                TimestampWebService service = new TimestampWebService();
                cachedTimestamp = service.GetTime();
             }
             catch {
                throw new Exception(
                   "The TimestampWebService cannot be accessed. Make sure that it is running.");
             }
          }
          #endregion
       }
    }
    
  • Create an instance of the WebServiceTimestampStrategy class and assign it to the AuditTrailService.Instance.TimestampStrategy property. In the code below, this is performed for the Windows Forms application. Actually, you can do it for the ASP.NET application as well.

    static void Main() {
       MySolutionWindowsFormsApplication application = new MySolutionWindowsFormsApplication();
       //...
       IAuditTimestampStrategy timeStampStrategy = new WebServiceTimestampStrategy();
       AuditTrailService.Instance.TimestampStrategy = timeStampStrategy;
       application.Setup();
       //...
    }
    
  • Run the application. Check that the implemented code works.

MSSql Server Timestamp Strategy

To get the time, you can use the MSSql server. For this purpose, use the Session.ExecuteScalar method. To get time on the MSSql server, use the GETDATE() function. The following code demonstrates the entire MSSqlServerTimestampStrategy class implementation:

using System;
using DevExpress.Xpo;
using DevExpress.Persistent.AuditTrail;
namespace MySolution.Module {
   public class MSSqlServerTimestampStrategy : IAuditTimestampStrategy{
      DateTime cachedTimestamp;
#region IAuditTimestampStrategy Members
      public DateTime GetTimestamp(AuditDataItem auditDataItem) {
         return cachedTimestamp;
      }
      public void OnBeginSaveTransaction(Session session) {
         cachedTimeStamp = (DateTime)session.ExecuteScalar("select getdate()");
      }
#endregion
   }
}

Create an instance of the MSSqlServerTimestampStrategy class and assign it to the AuditTrailService.Instance.TimestampStrategy property. It was demonstrated above for the WebServiceTimestampStrategy class.