通常,如果nhibernate和log4net同时出现的话,nhibernate通常会自动用log4net输出信息,这样的话,会和通过应用程序输出的日志混杂在一起。因为nhibernate会默认使用root下的appender。但是,我们可以通过配置其它的logger来控制nihibernate的输出,如下配置:(这个配置是log4net连接到sql2008的一个配置文件)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<appSettings/>
<connectionStrings/>
<system.web>
</system.web>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="ADONetAppender" />
</root>
<!--Nhibernate的日志配置开始-->
<!--additivity是 子Logger 是否继承 父Logger 的 输出源(appender) 的标志位。具体说,默认情况下子Logger会继承父Logger的appender,
也就是说子Logger会在父Logger的appender里输出。若是additivity设为false,则子Logger只会在自己的appender里输出,
而不会在父Logger的appender里输出。 -->
<logger name="NHibernate" additivity="false">
<level value="WARN" />
<appender-ref ref="ADONetAppender" />
</logger>
<logger name="NHibernate.SQL" additivity="false">
<level value="ERROR" />
<appender-ref ref="ADONetAppender" />
</logger>
<!--Nhibernate的日志配置结束-->
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="0" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="server=localhost;database=SummerData;user id=sa;password=P@ssw0rd;Connect Timeout=15;" />
<commandText value="INSERT INTO LogMessage ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
</log4net>
</configuration>