PDA

View Full Version : Audit Logging with an Interceptor



mk77
02-27-2007, 04:45 PM
I would like to implement audit logging for my system that would automatically record who and when created/updated a record in the database. I created the following Hibernate Interceptor to update object properties prior to the object being saved to the database (partial code):



using System;
using System.Collections;
using NHibernate;
using NHibernate.Type;
using Common.Logging;

namespace Pohs.DAL
{

class AuditInterceptor : IInterceptor
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase. GetCurrentMethod().DeclaringType);

public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState,
string[] propertyNames, IType[] types)
{
log.Debug("FLUSH DIRTY");

for ( int i=0; i < propertyNames.Length; i++ )
{
if ("UpdatedDateTime" == propertyNames[i])
{
currentState[i] = DateTime.Now;
return true;
}
}

return false;
}

public bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
log.Debug("SAVE");

for ( int i=0; i<propertyNames.Length; i++ )
{
if ("CreatedDateTime" == propertyNames[i])
{
state[i] = DateTime.Now;
return true;
}
}

return false;
}

.........
.........
}
}


And I registered it with the HibernateTransactionManager using the entityInterceptor property like:



<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" xmlns:d="http://www.springframework.net/database">

<!-- Database Connection Provider -->
<d:dbProvider id="databaseProvider"
provider="System.Data.SqlClient"
connectionString="Server=Server;Database=Database;Trusted_Connection =Yes;"/>

<!-- Hibernate session factory -->
<object id="hibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="DbProvider" ref="databaseProvider"/>
<property name="MappingAssemblies">
<list>
<value>Pohs.DAL</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<entry key="hibernate.connection.isolation" value="ReadCommitted" />
<entry key="hibernate.default_schema" value="Pohs.dbo"/>
</dictionary>
</property>
</object>

<!-- Audit Logging Interceptor -->
<object id="auditInterceptor"
type="Pohs.DAL.AuditInterceptor, Pohs.DAL">
</object>

<!-- Hibernate transaction manager -->
<object id="hibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">
<property name="DbProvider" ref="databaseProvider"/>
<property name="sessionFactory" ref="hibernateSessionFactory"/>
<property name="entityInterceptor" ref="auditInterceptor"/>
</object>
<!-- Configure Spring AOP autoproxy support -->
<object id="autoProxy"
type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoP roxyCreator, Spring.Aop">
</object>
<object id="transactionAttributeSource"
type="Spring.Transaction.Interceptor.AttributesTransacti onAttributeSource, Spring.Data">
</object>
<!-- Transaction Interceptor based on attribute [Transaction()] -->
<object id="transactionInterceptor" type="Spring.Transaction.Interceptor.TransactionIntercep tor, Spring.Data">
<property name="TransactionManager" ref="hibernateTransactionManager"/>
<property name="TransactionAttributeSource" ref="transactionAttributeSource"/>
</object>
<object id="transactionAdvisor"
type="Spring.Transaction.Interceptor.TransactionAttribut eSourceAdvisor, Spring.Data"
autowire="constructor">
</object>

</objects>


However, I can't get the Interceptor to execute. Am I doing something worng?

Thanks,
Max

boriska
03-12-2007, 11:33 AM
Hello Max,

please see thread
http://forum.springframework.net/showthread.php?t=1791

I tested a solution where IInterceptor in injected on HibernateTemplate. It worked.

Regards,
Boris

gator3033
01-06-2008, 11:54 PM
I've followed the steps above but my interceptor is not being executed. We are using transactional proxy's for our service classes. I've include our spring config below. When I debug to my customer dao which simply contains
HibernateTemplate.Get<Domain.Customer.Customer>(customerId);
And I put into the immediate window
HibernateTemplate.SessionFactory.GetCurrentSession ().GetSessionImplementation().Interceptor
{NHibernate.EmptyInterceptor}
[NHibernate.EmptyInterceptor]: {NHibernate.EmptyInterceptor}
For some reason the interceptor on the session implementor is the EmptyInterceptor and not my Audit Interceptor. I should also note that the EntityInterceptor property on the HibernateTemplate is set to my AuditInterceptor.

My CustomerService class that is proxied only call the dao.
public Customer CustomerGetById(int id)
{
return CustomerDao.CustomerFetch(id);
}

From BusinessServices.xml
<object id="txProxyConfigurationTemplate" abstract="true" type="Spring.Transaction.Interceptor.TransactionProxyFac toryObject, Spring.Data">
<property name="PlatformTransactionManager" ref="hibernateTransactionManager"/>
<property name="TransactionAttributes">
<name-values>
<add key="*Create" value="PROPAGATION_REQUIRED"/>
<add key="*Save" value="PROPAGATION_REQUIRED"/>
<add key="*Update" value="PROPAGATION_REQUIRED"/>
<add key="*Modify" value="PROPAGATION_REQUIRED"/>
<add key="*Deactivate" value="PROPAGATION_REQUIRED"/>
<add key="*Delete" value="PROPAGATION_REQUIRED"/>
<add key="*Remove" value="PROPAGATION_REQUIRED"/>
<add key="ExecuteMethod" value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
</object>

<object id="customerService" parent="txProxyConfigurationTemplate">
<property name="Target">
<object id="customerServiceTarget" type="...CustomerService, BusinessServices" singleton="true" parent="BusinessServiceBase">
<property name="customerDao" ref="customerDao"/>
</object>
</property>
</object>


From Dao.xml
<object id="hibernateTemplate" type="Spring.Data.NHibernate.Generic.HibernateTemplate">
<property name="SessionFactory" ref="sessionFactory"/>
<property name="TemplateFlushMode" value="Commit"/>
<property name="QueryCacheRegion" value="Hibernate.Query"/>
<property name="CacheQueries" value="true"/>
<property name="EntityInterceptor" ref="entityInterceptor"/>
</object>

<alias alias="EntityInterceptor" name="entityInterceptor"/>
<object id="entityInterceptor" type="...AuditInterceptor">
</object>

<!-- Hibernate Transaction Manager -->
<object id="hibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">
<property name="DbProvider" ref="dbProvider"/>
<property name="SessionFactory" ref="sessionFactory"/>
<property name="EntityInterceptor" ref="entityInterceptor"/>
</object>

<alias alias="SessionFactory" name="sessionFactory"/>

<object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12" singleton="true">
<property name="DbProvider" ref="dbProvider"/>
<property name="MappingAssemblies">
<list>
<value>HibernateDao</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
</dictionary>
</property>
</object>


From Web.Config
<appSettings>
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewMo dule.SessionFactoryObjectName" value="SessionFactory"/>
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewMo dule.EntityInterceptorObjectName" value="EntityInterceptor"/>
</appSettings>

<httpModules>
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web" />
</httpModule>

aanensonex
06-20-2009, 07:51 AM
An auditor wants to estimate what proportion of a bank’s commercial loan files are incomplete.? An auditor wants to estimate what proportion of a bank’s commercial loan files are incomplete. The auditor wants to be within 10% of the true proportion when using a 95% confidence level. How many files must the auditor sample? No estimate of the proportion is available, so use 0.5 for the population proportion.
__________________
affiliateelite (http://www.keywordspy.com/overview/domain.aspx?q=affiliateelite.com) ~ affiliateelite.com (http://www.keywordspy.com/overview/domain.aspx?q=affiliateelite.com) ~ adgooroo (http://www.keywordspy.com/overview/keyword.aspx?q=adgooroo) ~ adgooroo.com (http://www.keywordspy.com/overview/domain.aspx?q=adgooroo.com)