<p>I'm using NHibernate on a project and I need to do data auditing.  I found <a href="http://www.codeproject.com/KB/cs/NHibernate_IInterceptor.aspx" rel="nofollow noreferrer">this article</a> on codeproject which discusses the IInterceptor interface.</p>

<p>What is your preferred way of auditing data?  Do you use database triggers?  Do you use something similar to what's dicussed in the article?</p>

[{'answer_id': 15940, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>I prefer the CodeProject approach you mentioned.</p>\n\n<p>One problem with database triggers is that it leaves you no choice but to use Integrated Security coupled with ActiveDirectory as access to your SQL Server. The reason for that is that your connection should inherit the identity of the user who triggered the connection; if your application uses a named "sa" account or other user accounts, the "user" field will only reflect "sa".</p>\n\n<p>This can be overriden by creating a named SQL Server account for each and every user of the application, but this will be impractical for non-intranet, public facing web applications, for example.</p>\n'}, {'answer_id': 16061, 'author': 'DavidWhitney', 'author_id': 1297, 'author_profile': 'https://Stackoverflow.com/users/1297', 'pm_score': 3, 'selected': False, 'text': "<p>[EDIT]</p>\n\n<p>Post NH2.0 release, please look at the Event Listeners as suggested below.  My answer is outdated.</p>\n\n<hr>\n\n<p>The IInterceptor is the recommended way to modify any data in nhibernate in a non-invasive fashion.  It's also useful for decryption / encryption of data without your application code needing to know.</p>\n\n<p>Triggers on the database are moving the responsibility of logging (an application concern) in to the DBMS layer which effectively ties your logging solution to your database platform.  By encapsulating the auditing mechanics in the persistance layer you retain platform independance and code transportability.</p>\n\n<p>I use Interceptors in production code to provide auditing in a few large systems.</p>\n"}, {'answer_id': 17752, 'author': 'Ian Nelson', 'author_id': 2084, 'author_profile': 'https://Stackoverflow.com/users/2084', 'pm_score': 2, 'selected': False, 'text': "<p>I do like the Interceptor approach mentioned, and use this on the project I'm currently working on.</p>\n\n<p>However, one obvious disadvantage that deserves highlighting is that this approach will only audit data changes made via your application. Any direct data modifications such as ad-hoc SQL scripts that you may need to execute from time to time (it always happens!) won't be audited, unless you remember to perform the audit table insertions at the same time.</p>\n"}, {'answer_id': 142767, 'author': 'Sean Carpenter', 'author_id': 729, 'author_profile': 'https://Stackoverflow.com/users/729', 'pm_score': 5, 'selected': True, 'text': '<p>For NHibernate 2.0, you should also look at <a href="http://knol.google.com/k/fabio-maulo/nhibernate-chapter-11/1nr4enxv3dpeq/14#view" rel="noreferrer">Event Listeners</a>.  These are the evolution of the IInterceptor interface and we use them successfully for auditing.</p>\n'}, {'answer_id': 212932, 'author': 'kͩeͣmͮpͥ ͩ', 'author_id': 26479, 'author_profile': 'https://Stackoverflow.com/users/26479', 'pm_score': 2, 'selected': False, 'text': "<p>As an entirely different approach, you could use the decorator pattern with your repositories. </p>\n\n<p>Say I have </p>\n\n<pre><code>public interface IRepository&lt;EntityType&gt; where EntityType:IAuditably\n{ \n    public void Save(EntityType entity);\n}\n</code></pre>\n\n<p>Then, we'd have our NHibernateRepository:</p>\n\n<pre><code>public class NHibernateRepository&lt;EntityType&gt;:IRepository&lt;EntityType&gt;\n{\n   /*...*/\n   public void Save ( EntityType entity )\n   {\n       session.SaveOrUpdate(entity);\n   }\n}\n</code></pre>\n\n<p>Then we could have an Auditing Repository:</p>\n\n<pre><code>public class AuditingRepository&lt;EntityType&gt;:IRepository&lt;EntityType&gt;\n{\n   /*...*/\n   public void Save ( EntityType entity )\n   {\n       entity.LastUser = security.CurrentUser;\n       entity.LastUpdate = DateTime.UtcNow;\n       innerRepository.Save(entity)\n   }\n}\n</code></pre>\n\n<p>Then, using an IoC Framework (StructureMap, Castle Windsor, NInject) you could build it all up without the rest of your code every knowing you had auditing going on.</p>\n\n<p>Of course, how you audit the elements of cascaded collections is another issue entirely...</p>\n"}, {'answer_id': 911638, 'author': 'Rohit Agarwal', 'author_id': 108955, 'author_profile': 'https://Stackoverflow.com/users/108955', 'pm_score': 2, 'selected': False, 'text': '<p>I understand this is an old question. But I would like to answer this in the light of the new Event System in NH 2.0. Event Listeners are better for auditing-like-functions than Interceptors. Ayende wrote a great example on his blog last month. Here\'s the URL to his blog post - </p>\n\n<p><a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx" rel="nofollow noreferrer">ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx</a></p>\n'}]
['https://Stackoverflow.com/questions/15917', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1122/']