Question

i've hooked up logging in my application to automatically log changes to certain entities using event listeners. This works great but for some of the properties withing the entities i'm logging i don't wish to insert a log if only a change is made to that property. These properties are decorated with the IgnoreLoggingAttribute attribute. Here is what i have so far:

public void OnPostUpdate(PostUpdateEvent @event)
{
    var session = @event.Session.GetSession(NHibernate.EntityMode.Poco);

    if (@event.Entity is User)
        session.SaveOrUpdate(new UserLog((User)@event.Entity));
    ...
}

The @event exposes 2 properties which are called State and OldState. I can use this to check for changes however i can't pull out the properties i'm interested in since these are simply an object arrays. I figured i could use some reflection to get the all the indexes (for any properties with the IgnoreLoggingAttribute) and match them up with the ones in the object array. So far i have come up with:

var properties = typeof(User).GetProperties().Where(p => p.GetCustomAttributes(typeof(IgnoreLoggingAttribute), false).Count() > 0);

The problem now is it doesn't give me the index of the property against the original entity. I also need to make sure this matches the appropriate index from the @event.State and @event.OldState properties (which both seem to ignore certain properties).

Was it helpful?

Solution

I was able to achieve this by calling this method before inserting into the log table:

private bool IsDirty(PostUpdateEvent @event) {
    // Get all the mapped property names
    var propertyNames = @event.Session.Factory.GetClassMetadata(@event.Entity.GetType()).PropertyNames;

    // Get the property index to ignore
    var propertyIndexesToIgnore = @event.Entity.GetType().GetProperties()
        .Where(p => p.GetCustomAttributes(typeof(IgnoreLoggingAttribute), false).Count() > 0)
        .Select(p => Array.IndexOf(propertyNames, p.Name)).ToArray();

    if (@event.OldState != null && @event.State != null) {
        for (var i = 0; i < @event.OldState.Length; i++) {
            if (!propertyIndexesToIgnore.Contains(i) && !@event.OldState[i].Equals(@event.State[i]))
                return true;
        }
    }

    return false;
}

Now all i have to do is apply the IgnoreLoggingAttribute attribute to any properties i don't wish to log. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top