登录NHibernate的SQL查询 [英] Logging NHibernate SQL queries

查看:232
本文介绍了登录NHibernate的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来访问完整的SQL查询,包括价值观,在我的code?

Is there a way to access the full SQL query, including the values, inside my code?

我能够登录使用log4net的SQL查询:

I am able to log SQL queries using log4net:

<logger name="NHibernate.SQL" additivity="false">
    <level value="ALL"/>
    <appender-ref ref="NHibernateSQLFileLog"/>
</logger>

不过,我想找到一种方法来记录从code也SQL查询。这样,我将记录特定的SQL查询,在我的try / catch语句导致异常。

However, I would like to find a way to log SQL queries from the code also. This way I will log the specific SQL query that causes an exception in my try/catch statement.

现在我要进行数据挖掘的SQLFileLog发现导致异常的查询,当异常发生时,它是没有效率。

Right now I have to data-mine the SQLFileLog to find the query that caused the exception when an exception occurs and it is not efficient.

推荐答案

您可以使用拦截器来做到这一点:

you can use an interceptor to do this:

public class LoggingInterceptor : EmptyInterceptor {
    public override SqlString OnPrepareStatement(SqlString sql) {

        Debug.WriteLine(sql);

        return sql;
    }
}

请参阅 NHibernate的文档了不同的方式来与NHibernate注册。

See Nhibernate Docs for the different ways to register it with nhibernate.

这篇关于登录NHibernate的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆