0

I wanted to get your opinions on the easiest way to track changes that users make when they do CRUD events. I am working on a system where the users are less interested on permissions, but really want to have a sophisticated log of what changes a user made. I am using ASP.NET MVC 3, EF, and NLog.

Any advice is greatly appreciated :)

Steve

Steve3p0
  • 1,762
  • 4
  • 18
  • 25
  • 1
    Check out [Implementing Audit Log / Change History with MVC & Entity Framework](http://stackoverflow.com/questions/6867459/implementing-audit-log-change-history-with-mvc-entity-framework/6867622#6867622) – Eranga Nov 19 '11 at 23:42

1 Answers1

2

I use a convention based approach. Each entity has an associated audit entity which includes all properties from the base entity plus information on the change, including whether it was successful or not. I override the SaveChanges method on the DB context. For each entity being changed it creates an audit entity from it holding the new values. It attempts to save the changes, then uses a separate, auditing context to save each of the audited entities with the results of the save operation. I use an injected utility in the data context to get access to the current user (via HttpContext.Current for web, via the Environment.User for non-web) when constructing the audit entities.

I blogged about an earlier version of this for LINQ to SQL at http://farm-fresh-code.blogspot.com/2009/05/auditing-inserts-and-updates-using-linq.html. You should be able to get the basic idea from that.

tvanfosson
  • 490,224
  • 93
  • 683
  • 780
  • Thank you tvanfosson! I will take a look at it. Do you know anything about Self Tracking Entities? Can this be used for auditing and tracking purposes? – Steve3p0 Nov 20 '11 at 21:15