Audit. Getting Started

Note

For correct work of the Audit on the MS SQL it is required that the database user (or role) has the permission to select from the system sys.dm_tran_current_transaction table. The VIEW SERVER STATE permission for this table is also required.

To grant these permissions for the PUBLIC role, use the following script:

  • example

use [master]
 GRANT SELECT ON [sys].[dm_tran_current_transaction] TO [public]
 Go
 GRANT VIEW SERVER STATE TO [public]
 GO

To use Xafari Audit in XAF-applications, execute the following steps:

Add Xafari.BC.Audit, Xafari.BC.Audit.Triggers and Xafari.BC.Audit.Cfg modules.

To display Audit_cfg_dashboard and Settings_cfg_dashboard correctly, open the Model Editor (or the Model.xafml file of the target application in Visual Studio), navigate to the NavigationItems node, and set the value of the NavigationStyle property to TreeList (see the image below).

audit_13

To display Audit data, add the Xafari.BC.Audit.Win module.

Add Xafari.BC.Settings module to support Audit Settings.

Audit processes only types that implements the IAuditSupport interface, implementation is described below.

When you first start the application framework generates a list of types that satisfy the following requirements:

  • IAuditSupport interface is implemented,
  • Type is not decorated with NotAudited and DeferredDeletion (false) attributes.

Configure Audit in application. Execute Apply audit settings Action to enable Audit and make all the necessary database settings.

You can examine a simple Audit implementation sample in one of the earlier versions of Xafari (x07) in the Xafari Audit. Getting Started post.

Examples of IAuditSupport interface implementation.

Xafari.Base.IAuditSupport interface is declared in the Xafari.dll assembly.

Domain Component:

  • c#
  • VB

[DomainComponent]
[DefaultClassOptions]
public interface MainDC : XafariObject
{
  int IntValue { get; set; }
  string StringValue { get; set; }
}

<DomainComponent> _
<DefaultClassOptions> _
Public Interface MainDC
  Inherits XafariObject
  Property IntValue As Integer
  Property StringValue As String
End Interface

XPO:

  • c#
  • VB

[DefaultClassOptions]
public class XpoMyObject : BaseObject, IAuditSupport
{
  public XpoMyObject(Session session)
    : base(session)
  {
  }
  private int _intValue;
  public int IntValue
  {
    get
    {
      return _intValue;
    }
    set
    {
      SetPropertyValue("IntValue", ref _intValue, value);
    }
  }
  private string _stringValue;
  public string StringValue
  {
    get
    {
      return _stringValue;
    }
    set
    {
      SetPropertyValue("StringValue", ref _stringValue, value);
    }
  }
  public override void AfterConstruction()
  {
    base.AfterConstruction();
    HistoryCreate = new ActionInfo { ActionExecutor = SecuritySystem.CurrentUserName, Date = DateTime.Now };
  }
  protected override void OnChanged(string propertyName, object oldValue, object newValue)
  {
    base.OnChanged(propertyName, oldValue, newValue);
    HistoryModify = new ActionInfo { ActionExecutor = SecuritySystem.CurrentUserName, Date = DateTime.Now };
  }
  private ActionInfo _historyCreate;
  [Persistent]
  public ActionInfo HistoryCreate
  {
    get
    {
      return _historyCreate;
    }
    set
    {
      SetPropertyValue("HistoryCreate", ref _historyCreate, value);
    }
  }
  private ActionInfo _historyModify;
  [Persistent]
  public ActionInfo HistoryModify
  {
    get
    {
      return _historyModify;
    }
    set
    {
      SetPropertyValue("HistoryModify", ref _historyModify, value);
    }
  }
  private ulong _transactionId;
  public ulong TransactionId
  {
    get
    {
      return _transactionId;
    }
    set
    {
      SetPropertyValue("TransactionId", ref _transactionId, value);
    }
  }
}

<DefaultClassOptions> _
Public Class XpoMyObject
  Inherits BaseObject
  Implements IAuditSupport
  Public Sub New(ByVal session As Session)
    MyBase.New(session)
  End Sub
  Private __intValue As Integer
  Public Property IntValue As Integer
    Get
      Return __intValue
    End Get
    Set
      SetPropertyValue("IntValue", __intValue, value)
    End Set
  End Property
  Private __stringValue As String
  Public Property StringValue As String
    Get
      Return __stringValue
    End Get
    Set
      SetPropertyValue("StringValue", __stringValue, value)
    End Set
  End Property
  Public Overrides Sub AfterConstruction()
    MyBase.AfterConstruction()
    HistoryCreate = New ActionInfo() With {.ActionExecutor = SecuritySystem.CurrentUserName, .Date = DateTime.Now}
  End Sub
  Protected Overrides Sub OnChanged(ByVal propertyName As String, ByVal oldValue As Object, ByVal newValue As Object)
    MyBase.OnChanged(propertyName, oldValue, newValue)
    HistoryModify = New ActionInfo() With {.ActionExecutor = SecuritySystem.CurrentUserName, .Date = DateTime.Now}
  End Sub
  Private __historyCreate As ActionInfo
  <Persistent> _
Public Property HistoryCreate As ActionInfo
    Get
      Return __historyCreate
    End Get
    Set
      SetPropertyValue("HistoryCreate", __historyCreate, value)
    End Set
  End Property
  Private __historyModify As ActionInfo
  <Persistent> _
Public Property HistoryModify As ActionInfo
    Get
      Return __historyModify
    End Get
    Set
      SetPropertyValue("HistoryModify", __historyModify, value)
    End Set
  End Property
  Private __transactionId As ulong
  Public Property TransactionId As ulong
    Get
      Return __transactionId
    End Get
    Set
      SetPropertyValue("TransactionId", __transactionId, value)
    End Set
  End Property
End Class