如何使用 Log4Net 从 NHibernate 获取真实的 sql
我决定学习如何使用 ORM 来避免学习一些 SQL(错误——唯一的方法就是不使用 SQL)。
我已经能够使用 Log4Net 获取 Nhibernate“SQL”,使用在很多博客中重复的说明。我得到的“SQL”是这样的:
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_
如果我错了,请纠正我,但这不是 SQL,我不明白为什么所有这些博客都这么说。
奇怪的是,早些时候当我摆弄 log4net 时,我确信我能够将普通 SQL 记录到日志文件中。当我基本上对一个实体执行 getall() 操作(读取整个表)时,所有单独的查询都会在那里列出,查询中的 id 对应每一行(实体)。我绝对没有想到这一点。谁能告诉我这是如何用 log4net 完成的?这是我现在的配置:
<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>
编辑:我现在知道它是 sql,并且我无法重现我之前看到的发出的 sql,因为之前启用了延迟加载:
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)]
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)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在日志中的破折号后面看到的确实是 SQL。它在语法和语义上都是正确的,但它看起来很丑陋。当 SQL 不是手动编写时,这种情况很常见:代码生成器使用诸如
RegionID9_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.将第 4 行的 conversionPattern 从: 更改
为
,您将仅记录 SQL(它是 %message 的值)
,日志中的以下行是 SQL:
change the conversionPattern on the 4th line from:
to
and you will log only the SQL (it's the value of %message)
the following line, from your log, is SQL: