使用实体框架中的 ObjectContext.SaveChanges 方法有选择地审核日志记录
我需要在应用程序中执行审核日志记录,并且希望 SaveChanges
记录对数据库的任何更改。在某些情况下,我不希望发生日志记录。告诉 SaveChanges 不执行审核日志记录的最优雅的方式是什么?我正在使用实体框架 4。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建一个自定义属性来装饰需要日志的元素。它将通过 log 方法读取。此属性可以应用于类和属性,以便您可以精确选择应记录的内容。
我建议您使用缓存来避免每次都检查是否应该记录某些内容。
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.
我已经实现了类似的日志记录要求。我将表的信息存储在数据库中名为 tablemaster 的元表中,并存储与登录相关的大量信息。其中一个字段指示是否记录该表的条目。
当在上下文中调用保存更改时,我会记录审核跟踪,即当我决定是否要对这些表进行日志记录时。
元表 tablemaster 在应用程序启动时被提取到内存中,因此无需往返检查详细信息。希望这有帮助。
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.