如何使用NHibernate将SQL调用记录到Visual Studio的控制台?(How to log SQL calls with NHibernate to the console of Visual Studio?)
我有以下NHibernate的配置文件:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.connection_string">Server=.\SQLEXPRESS;Database=mydb;Integrated Security=True;</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.release_mode">auto</property> <property name="adonet.batch_size">500</property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>但SQL不会显示在Visual Studio的输出窗口中。 是否强制安装log4net ? 还是应该show_sql单独工作?
I have the following configuration file for NHibernate:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.connection_string">Server=.\SQLEXPRESS;Database=mydb;Integrated Security=True;</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.release_mode">auto</property> <property name="adonet.batch_size">500</property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>But the SQL doesn't show in the output window of Visual Studio. Is it mandatory to install log4net? Or should show_sql work alone?
最满意答案
要在Visual Studio的输出窗口中显示SQL,请在log4net配置中配置log4net以使用TraceAppender。 这个:
<appender name="DebugSQL" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>那么这个
<logger name="NHibernate.SQL" additivity="false"> <level value="DEBUG" /> <appender-ref ref="DebugSQL" /> </logger>编辑:我似乎无法正确格式化这里。 有关代码示例,请参阅此链接
To show the SQL in the output window of Visual Studio, configure log4net to use TraceAppender in your log4net config. This:
<appender name="DebugSQL" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>Then this:
<logger name="NHibernate.SQL" additivity="false"> <level value="DEBUG" /> <appender-ref ref="DebugSQL" /> </logger>EDIT: I can't seem to format this correctly here. See this link for code example
更多推荐
发布评论