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.

- 39,603
- 20
- 94
- 123

- 3
- 1
2 Answers
I have implemented similar requirement for logging. I store tables' info in a metatable called tablemaster in database, and store a lot of information related to logging in that. One of the fields tells whether to log entries for that table or not. I log Audit trail when saving changes is called in context, that is when I decide if logging for these tables is to be done or not. Meta Table tablemaster is fetched in the memory at start of App, so no round trips for checking details. Hope this helps.

- 324
- 3
- 8
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.

- 9,389
- 6
- 45
- 65