简体   繁体   English

如何使用NLog / Log4Net自动执行数据库更改跟踪?

[英]How to use NLog/Log4Net to automate database change tracking?

I am aware of many table auditing methods. 我知道许多表审核方法。 Some are trigger based and require triggers on each table to be audited. 有些是基于触发器的,并且需要审核每个表上的触发器。 SOme have limitation based on column datatype. SOme具有基于列数据类型的限制。 How can I use a logging framework like NLog or Log4Net. 如何使用NLog或Log4Net之类的日志记录框架。 I want my log(in a table) to answer question " Who updated this column of this table from val0 to val1 at this time?" 我希望我的日志(在表中)回答问题“谁这次将表的此列从val0更新为val1?”

I have not decided on a OR mapper yet. 我还没有决定使用OR映射器。 Inclining towards NHibernate or EF. 倾向于NHibernate或EF。

Thanks in advance ! 提前致谢 !

If you use a pattern like DAO (I use it successfully in Java with Hibernate) you can add an entry for every change in the database. 如果您使用DAO之类的模式(我在Java中使用Hibernate成功地使用了它),则可以为数据库中的每个更改添加一个条目。 If you use Spring.NET you can use AOP for adding interceptors to the methods of the DAOs accessing the database. 如果使用Spring.NET ,则可以使用AOP将拦截器添加到访问数据库的DAO方法中。

Anyway, you might want to add a special appender for the DAOs package because it would be very verbose. 无论如何,您可能想为DAO包添加一个特殊的附加程序,因为它非常冗长。

Victors answer is top notch but I wanted to add that this may be hard depending on how your column->object mappings work. Victors的回答是一流的,但我想补充一点,这可能很难,具体取决于您的column-> object映射的工作方式。

If your mapping your table columns to object properties and fields with different names then I think you'll looking at trying to parse sql information to determine which column was updated. 如果将表列映射到具有不同名称的对象属性和字段,那么我认为您将尝试解析sql信息以确定更新了哪一列。 For example if your changed the value of "Product.Price" and that actually maps to a column named "final_price" your logging won't exactly match up to whats happening in the database. 例如,如果您更改了“ Product.Price”的值并且实际上映射到名为“ final_price”的列,则您的日志记录将与数据库中发生的情况不完全匹配。

Also this could be wrong but if your working with disconnected entities through an ORM package I'm not sure if change tracking information will be available. 同样,这可能是错误的,但是如果您通过ORM包处理断开连接的实体,则不确定更改跟踪信息是否可用。 I vaguely remember a problem I read about where one of the ORMs you listed were updating every column so it appeared a user changed all the columns in the table when they really updated a single column. 我模糊地记得我读过一个问题,该问题涉及您列出的一个ORM在哪里更新每一列,因此当用户真正更新单个列时,似乎用户更改了表中的所有列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM