Subject : Problem working on IAuditor

Jan 2, 2014 at 8:14 AM
Hi,

Working Environment : NOF 5.6, Net 4.0, VS2010, MVC4 and EF5
I have done the following steps:
1) In RunWeb class there is a method :
protected override IAuditorInstaller Auditor
    {
        get
        {
            return new AuditInstaller(new MyDefaultAuditor());

        }
    }
2) Create a class named ‘MyDefaultAuditor.cs’ as follows
public class MyDefaultAuditor : IAuditor
{

    public void ActionInvoked(IPrincipal byPrincipal, string actionName, string serviceName, bool queryOnly, object[] withParameters)
    {

       // throw new NotImplementedException();
    }

    public void ActionInvoked(IPrincipal byPrincipal, string actionName, object onObject, bool queryOnly, object[] withParameters)
    {
        //throw new NotImplementedException();
    }

    public void ObjectPersisted(IPrincipal byPrincipal, object updatedObject)
    {
        //throw new NotImplementedException();
    }

    public void ObjectUpdated(IPrincipal byPrincipal, object updatedObject)
    {
        //throw new NotImplementedException();
    }
}
3) Domain object ‘Account.cs’ is as follows:

public class Account : MyDefaultAuditor
    {
    public virtual int Id { get; set; }        
    public virtual string AccountNumber { get; set; }
}

Everything is ok. But data related to auditing for Account class are not persisted.
Would you please guide me what to do next to store the audit related info (ex: InsertedBy, InsertedDate, etc.).

Thanks,
Elma
Coordinator
Jan 2, 2014 at 1:58 PM
You have to do any persistence yourself so for example you could create a domain class

public class AccountAudit {
  public int Id {get;set;}
  public string AuditInfo {get;set;}
}
Then when an action is called you could create and save the info eg in MyDefaultAuditor
public void ActionInvoked(IPrincipal byPrincipal, string actionName, object onObject, bool queryOnly, object[] withParameters)
    {
        if (onObject is Account) {
            var auditInfo = string.Format("Action {0} called on Account {1}", actionName, ((Account)onObject).AccountNumber);

            var aa = Container.NewTransientInstance<AccountInfo>(); 
            aa.AuditInfo = auditInfo; 
            Container.Persist(ref aa); 
        }
    }
Warning - that should give you the idea it's not necessarily the best way of doing things I suggest you reread the documentation with the above in mind.
Jan 21, 2014 at 5:24 AM
Thanks for your suggestion. I've successfully worked with this.

Now I've another related question - how can i trace an update for a single field.
Here is the scenario:
In Domain class - CurrencyInformation, I've three properties - Currency, EffectiveDate and Rate.
public void ObjectUpdated(IPrincipal byPrincipal, object updatedObject)
{
}
If I update only the value of EffectiveDate, then how can I track that only the value of EffectiveDate has been updated with current Audit model.
Till now, we can track the insert/update for an entire record. Is this possible to track only the updated field? Do you have any suggestion?

Thanks in advance.
Coordinator
Jan 21, 2014 at 12:09 PM
One way you could do it would be to make use of the XMLSnapshotService, which creates a complete XML snapshot of an object. I commonly write an implementation of the IAuditor that snapshot the updatedObject and then persists this snapshot as part of an audit record against that object.

You could then compare a snapshot of the newly-updated object, with the previous snapshot (that you have persisted) for that object, and then, using XML manipulation methods, look for a particular field change.