问题描述
||
我在NHibernate 2.1中配置了PreInsert和PreDelete事件侦听器。对于特定表上的插入和删除,我想将审计事件写入单独的审计表。
public class AuditEventListener : IPreInsertEventListener,IPreDeleteEventListener
{
private static readonly ILog Log = LogManager.GetLogger(typeof(AuditEventListener));
public bool OnPreInsert(PreInsertEvent @event)
{
if (!(@event.Entity is IAuditable))
return false;
return AuditEvent(@event.Session.GetSession(EntityMode.Poco),true,(@event.Entity as IAuditable));
}
public bool OnPreDelete(PreDeleteEvent @event)
{
if (!(@event.Entity is IAuditable))
return false;
return AuditEvent(@event.Session.GetSession(EntityMode.Poco),false,(@event.Entity as IAuditable));
}
private bool AuditEvent(ISession session,bool isInsert,IAuditable entity)
{
if (entity is ArticleBinding)
{
if (Log.IsDebugEnabled)
Log.DebugFormat(\" audit event ({0}),entity is ArticleBinding\",((isInsert) ? \"Insert\" : \"Delete\"));
AddArticleBindingAuditEvent(session,isInsert,(entity as ArticleTagBinding));
}
return false;
}
private void AddArticleBindingAuditEvent(ISession session,ArticleBinding binding)
{
var auditRecord = new AuditArticleBinding()
{
ArticleId = binding.ArticleId,Action = (isInsert) ? \"Add\" : \"Delete\",LoggedBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name)
? \"Unknown\"
: Thread.CurrentPrincipal.Identity.Name
};
session.Save(auditRecord);
}
}
我解决了使用同一会话的问题,这导致了异常。现在,所有代码都运行良好,并且命中了我的日志语句,但是审核记录从未插入。使用NHProf,我可以看到INSERT调用永远不会发生。
上面的代码为什么不引起INSERT?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)