Version 3

    This page is out of date: see the NHibernate website for up-to-date information.

    For more information about Log4Net, check out its official homepage.

    NOTE: Beginning with NHibernate 1.0-rc1 you need to enable log4net logging manually - this means calling log4net.XmlConfigurator.Configure (or BasicConfigurator.Configure) at the start of your application. Previous versions of NHibernate did this internally, but the call was removed to give the user more control over logging configuration.

    Here is an example of log4net configuration in App.config:

    <?xml version="1.0" encoding="utf-8" ?>  
    <configuration>  
            <configSections>  
                <section name="nhibernate"  
                type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral,   
                PublicKeyToken=b77a5c561934e089" />  
                <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />  
        </configSections>  
          
        <nhibernate>  
            <!-- Put your nhibernate configuration here -->  
        </nhibernate>  
      
        <!-- This section contains the log4net configuration settings -->  
        <log4net debug="false">  
      
            <!-- Define some output appenders -->  
            <appender name="trace" type="log4net.Appender.TraceAppender, log4net">  
                <layout type="log4net.Layout.PatternLayout,log4net">  
                    <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />  
                </layout>  
            </appender>  
      
            <appender name="console" type="log4net.Appender.ConsoleAppender, log4net">  
                <layout type="log4net.Layout.PatternLayout,log4net">  
                    <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />  
                </layout>  
            </appender>  
      
            <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >  
                  
                <param name="File" value="log.txt" />  
                <param name="AppendToFile" value="true" />  
                <param name="RollingStyle" value="Date" />  
                <param name="DatePattern" value="yyyy.MM.dd" />  
                <param name="StaticLogFileName" value="true" />  
      
                <layout type="log4net.Layout.PatternLayout,log4net">  
                    <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />  
                </layout>  
            </appender>  
      
            <!-- Setup the root category, add the appenders and set the default priority -->  
              
            <root>  
                <priority value="INFO" />  
                <appender-ref ref="console" />  
            </root>  
        </log4net>  
    </configuration>
    

     

    This defines several appenders (log message destinations), but uses only one of them. Any messages with priority INFO or higher are output to the console. This makes for a lot of messages which are mostly only useful when debugging, so in production the minimum priority would be raised to WARN or ERROR.

    Here's another example, a .NET 2.0 web.config file:

    <?xml version="1.0"?>  
      
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">  
        <configSections>  
            <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,   
                Version=2.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />  
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />  
        </configSections>  
        <nhibernate>  
            <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />  
            <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />  
            <add key="hibernate.connection.connection_string"   
                value="Server=servername; Database=dbname; User=username; Password=secret;" />  
            <add key="hibernate.connection.isolation" value="ReadCommitted" />  
            <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />  
        </nhibernate>  
      
        <log4net>  
            <appender name="NHibernateFileLog" type="log4net.Appender.RollingFileAppender">  
                <file value="Logs/nhibernate.txt" />  
                <appendToFile value="true" />  
                <rollingStyle value="Size" />  
                <maxSizeRollBackups value="10" />  
                <maximumFileSize value="100KB" />  
                <staticLogFileName value="true" />  
                <layout type="log4net.Layout.PatternLayout">  
                    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />  
                </layout>  
            </appender>  
      
            <appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">  
                <file value="Logs/general.txt" />  
                <appendToFile value="true" />  
                <maximumFileSize value="100KB" />  
                <rollingStyle value="Size" />  
                <maxSizeRollBackups value="5" />  
                <layout type="log4net.Layout.PatternLayout">  
                    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />  
                </layout>  
            </appender>  
            <appender name="DataLog" type="log4net.Appender.RollingFileAppender">  
                <file value="Logs/data.txt" />  
                <appendToFile value="true" />  
                <maximumFileSize value="100KB" />  
                <rollingStyle value="Size" />  
                <maxSizeRollBackups value="5" />  
                <layout type="log4net.Layout.PatternLayout">  
                    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />  
                </layout>  
            </appender>  
      
            <!-- levels: DEBUG, INFO, WARN, ERROR, FATAL -->  
              
            <root>  
                <level value="DEBUG"/>  
                <appender-ref ref="GeneralLog" />  
            </root>  
              
            <logger name="NHibernate" additivity="false">  
                <level value="DEBUG"/>  
                <appender-ref ref="NHibernateFileLog"/>  
            </logger>  
            <logger name="Pushable.Data" additivity="false">  
                <level value="DEBUG"/>  
                <appender-ref ref="DataLog"/>  
            </logger>  
        </log4net>  
    </configuration>