无法阻止 NHibernate 在单元测试中将日志写入控制台

Can't stop NHibernate from writing log to console in unit tests

我有一些在本地数据库上使用 nhibernate 的单元测试。

我并不总是需要 show_sql 输出,所以我主要想停用它。但是无论我怎么设置属性,要么在测试项目的App.config

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="connection.connection_string_name">test</property>
        <property name="show_sql">false</property>
        <mapping assembly="MyLib"/>
    </session-factory>
</hibernate-configuration>

或者通过测试中的配置类

_logger.Info("Configuring NHibernate");
_configuration = new Configuration().Configure();
_configuration.SetProperty("nhibernate.show_sql", "false");
_sessionFactory = _configuration.BuildSessionFactory();

控制台输出 总是 显示 SQL 语句,使控制台变得混乱。

我错过了什么?我是不是设置错了?

原来还有一点可以控制输出;直接在方法上!

SchemaExport export = new SchemaExport(_configuration);
export.Create(true, true);

Create 方法提供了两个参数:

bool useStdOut, bool execute

第一个定义 SQL 是否写入标准输出,第二个控制它是否针对数据库执行。

在 copy/pasting 教程中,我总是将两者都设置为 true,这会覆盖我之前配置的所有内容。

将第一个参数设置为 false 后,我不再得到不需要的输出。