Nhibernate show_sql = false无法正常工作

时间:2011-06-23 17:03:46

标签: nhibernate fluent-nhibernate log4net

这就是我使用Fluent Nhibernate创建Session Factory的方法

    public static ISessionFactory CreateSessionFactory()
        {
            FluentNHibernate.Cfg.FluentConfiguration fconfig;


            fconfig = FluentNHibernate.Cfg.Fluently.Configure()
                        .Cache(c=>c.UseQueryCache())
                        .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008 
                                        .ConnectionString(c => c.FromConnectionStringWithKey("Database"))
                        .ProxyFactoryFactory<NHibernate.ByteCode.LinFu.ProxyFactoryFactory>()
                        .Mappings(
                                  m =>  m.FluentMappings
                                        .AddFromAssemblyOf<NHibernateRepositoryRegistry>()
                                        .Conventions.Add<Conventions.PrimaryKeyConvention>()
                                        .Conventions.Add<Conventions.IdForeignKeyConvention>()
                                        .Conventions.Add<Conventions.ReferenceForeignKeyConstraintNameConvention>()
                            )
                   //  i have tried this but it did not work
                   //.ExposeConfiguration(config=>{config.SetProperty("show_sql", "false");});
           return fconfig.BuildSessionFactory();
        }

这是我在Web.config

中的log4net配置

    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
        <file value="nhibernate.log" />
        <appendToFile value="true" />
        <maximumFileSize value="100KB" />
        <maxSizeRollBackups value="2" />

        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level %thread %logger - %message%newline" />
        </layout>
    </appender>
    <appender name="RollingFile2" type="log4net.Appender.RollingFileAppender">
        <file value="nhibernatesql.log" />
        <appendToFile value="true" />
        <maximumFileSize value="100KB" />
        <maxSizeRollBackups value="2" />

        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level %thread %logger - %message%newline" />
        </layout>
    </appender>
    <logger name="NHibernate">
        <level value="INFO" />
        <appender-ref ref="RollingFile" />
    </logger>


    <logger name="NHibernate.SQL">
        <level value="ALL" />
        <appender-ref ref="RollingFile2" />
    </logger>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingFile" />
    </root>

</log4net>  

我以前有过ShowSql()设置

我试图通过web.config appsettings设置它,以便我可以根据配置打开或关闭

所以我删除了ShowSql()并尝试了以下(现在我只是传递“false”值)

.ExposeConfiguration(config=>{config.SetProperty("show_sql", "false");});

也尝试了

.ExposeConfiguration(config=>{config.SetProperty("hibernate.show_sql", "false");});

但我仍然在nhibernatesql.log文件中生成sql

我正在使用NHibernate v3.1.0.4000和FluentNHibernate v1.2.0.712以及ASP.NET MVC3

有谁知道为什么sql仍在生成?

show_sql或ShowSql()是否仅用于Console.output目的,sql是否发送到log4net无论如何?

如果将ShowSQL设置为true

,我主要担心的是性能下降

不确定我是否在其他地方设置ShowSql

1 个答案:

答案 0 :(得分:7)

好吧我想我得到了答案

基于http://www.davesquared.net/2008/01/viewing-sql-generated-by-nhibernate.html

show_sql仅用于Console.out目的,sql被发送到log4net,无论如何..这就是我在日志中看到sql的原因