I decided to learn how to use an ORM to avoid learning some SQL (mistake — only approach would be to go no-SQL).
I have been able to get the Nhibernate “SQL” using Log4Net, using the instructions that are duplicated in quite a few blogs. I get “SQL” like this:
NHibernate.Loader.Loader: 2011-11-11 15:03:14,348 [9] INFO NHibernate.Loader.Loader [(null)] - SELECT this_.RegionID as RegionID9_0_, this_.RegionDescription as RegionDe2_9_0_ FROM Region this_
Now correct me if I am wrong but that is not SQL, and I can’t understand why all of these blogs talk like it is.
The strange thing is that earlier when I was messing around with log4net, I am sure that I was able to get ordinary SQL logged to a logfile. When I basically did getall() of an entity (read a whole table), all of the individual queries were listed there with the id in the query -one for each row(entity). I definitely didn’t imagine this. Can anyone tell me how this is done with log4net? Here is my config right now:
<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>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="log.txt"/>
<param name="AppendToFile" value="true"/>
<param name="DatePattern" value="yyyy.MM.dd"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionpattern value="%d %p %m%n">
</conversionpattern>
</layout>
</appender>
<logger name="NHibernate.Loader.Loader" additivity="false">
<level value="All"/>
<appender-ref ref="DebugSQL" />
</logger>
Edit: I now know that it was sql, and I couldn’t reproduce the emitted sql that I had seen earlier because lazyloading was on before:
NHibernate.SQL: SELECT region_.RegionDescription as RegionDe2_9_ FROM Region region_ WHERE region_.RegionID=@p0;@p0 = 1 [Type: Int32 (0)]
NHibernate.SQL: SELECT region_.RegionDescription as RegionDe2_9_ FROM Region region_ WHERE region_.RegionID=@p0;@p0 = 2 [Type: Int32 (0)]
NHibernate.SQL: SELECT region_.RegionDescription as RegionDe2_9_ FROM Region region_ WHERE region_.RegionID=@p0;@p0 = 3 [Type: Int32 (0)]
NHibernate.SQL: SELECT region_.RegionDescription as RegionDe2_9_ FROM Region region_ WHERE region_.RegionID=@p0;@p0 = 4 [Type: Int32 (0)]
What you see after the dash in the log is indeed SQL. It is syntactically and semantically correct, but it just looks plain ugly. This is commonplace when SQL is not written manually: code generators use names such as
RegionID9_0_for disambiguation, making the output look unusual to a human reader.