Auditing

Watch the Auditing video on YouTube.

What data changed, what was it, and what is it now? PLINQO auditing answers these questions without the work auditing normally takes.

When AuditingEnabled is true on the data context, PLINQO captures the changes each time you submit changes to the database. It records inserts, updates and deletes, and only the objects that changed, and only the properties of those objects that changed, with their before and after values. The audit is in the context's LastAudit property, and its ToXml() method turns the AuditLog into XML that's easy to store.

To turn auditing on by default, set the AuditingEnabled property of the Entities template. To leave a property out of the audit, add the NotAudited attribute to it in the entity's metadata class.

In this example, a new Priority is inserted and a Task is changed, then the changes are submitted:

using (var context = new TrackerDataContext())
{
    Priority p = new Priority();
    p.Name = "High!";
    context.Priority.InsertOnSubmit(p);

    Task t = context.Task.GetByKey(1);
    t.Details = "Startup Counterstrike. Used PLINQO. Project is done";

    context.SubmitChanges();
    AuditLog audit = context.LastAudit;
}

This is the audit as XML:

<audit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="http://schemas.codesmithtools.com/datacontext/audit/1.0">
  <entity action="Insert" type="Tracker.Data.Priority">
    <key name="Id" type="System.Int32">
      <value xsi:type="xsd:int">0</value>
    </key>
    <property name="Id" type="System.Int32">
      <current xsi:type="xsd:int">0</current>
    </property>
    <property name="Name" type="System.String">
      <current xsi:type="xsd:string">High!</current>
    </property>
    <property name="Order" type="System.Int32">
      <current xsi:type="xsd:int">0</current>
    </property>
  </entity>
  <entity action="Update" type="Tracker.Data.Task">
    <key name="Id" type="System.Int32">
      <value xsi:type="xsd:int">1</value>
    </key>
    <property name="Details" type="System.String">
      <current xsi:type="xsd:string">Startup CounterStrike. Used PLINQO. Project is done</current>
      <original xsi:type="xsd:string">Work overtime to get project started</original>
    </property>
  </entity>
</audit>