Search code examples
entity-frameworkauditingsavechanges

Selectively Audit Logging with the ObjectContext.SaveChanges method in Entity Framework


I need to perform audit logging in my application and I want SaveChanges to log any changes to the database. There are certain instances where I don't want the logging to occur. What would be the most elegant way of telling SaveChanges to not perform the audit logging? I'm using Entity Framework 4.


Solution

  • I would create an custom attribute to decorate the elements requiring log. It would be read by the log method. This attribute could be applied on class and properties so that you could select precisely what should be logged.

    I suggest you use caching to avoid checking every time if something should be logged or not.