I’m currently developing a web application in ASP.NET MVC and NHibernate. Since the application was very slow at some points I wanted to know which SQL queries NHibernate was executing. Since the log4net library was already included in the application I searched for a solution to output the SQL queries to the console in Visual Studio 2010 using log4net. I found the solution on this wiki and this blog post.
First thing you need to do is add a log4net section in your web.config if you don’t have one yet.
<configSections> ... <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> ... </configSections>
Then add the log4net section, and configure an appender and logger.
<log4net> <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> </log4net>
That’s all. Don’t forget to configure log4net in the Global.asax.
protected void Application_Start() { log4net.Config.XmlConfigurator.Configure(); ... }