Castle ActiveRecord: What's the easiest way to see the SQL?
I'm trying to get Castle ActiveRecord to show me the SQL it generates. The various blogs I've found on this give two alternatives:
(1) Use the NHibernate "show_sql" setting. The trouble is, I'm using programmatic configuration, like this.
var config = XmlConfigurationSource.Build(
DatabaseType.MsSqlServer2008, Se开发者_如何转开发ttings.Default.StationManagerDbConnectionString);
config.IsRunningInWebApp = isRunningInWebApp;
config.PluralizeTableNames = true;
var modelAssembly = Assembly.GetAssembly(typeof(OneOfMyClasses));
ActiveRecordStarter.Initialize(modelAssembly, config);
With programmatic configuration, there doesn't seem to be a way to specify "show_sql".
(2) Use log4net. But leaving aside what a pain log4net is to get working, I haven't found a way to get just the SQL. I get gobs and gobs of debug data, of which the SQL statements are just a small part.
So: Is there some way I can keep my programmatic configuration of Castle ActiveRecord but also get NHibernate to output just the SQL?
EDIT: Here's what I got to work with log4net. But in the first two pages of my web app, this spits out over 14,000 lines in the Debug window. How do I change this code to get only the SQL?
var appender = new log4net.Appender.DebugAppender
{
Layout = new log4net.Layout.SimpleLayout(),
Name = "NHibernate.SQL",
Threshold = log4net.Core.Level.Debug
};
log4net.Config.BasicConfigurator.Configure(appender);
You should be able to just separate out the sql by utilizing the NHibernate.SQL logger.
Example config:
<log4net>
<!-- This is a default logger that nhibernate uses to push out all the SQL statements to-->
<logger name="NHibernate.SQL" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="NHibernateSQLLog"/>
</logger>
<!-- This is a default logger that nhibernate uses to push out all the debugging type information to-->
<logger name="NHibernate" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="NHibernateFileLog"/>
</logger>
<appender name="NHibernateFileLog" type="log4net.Appender.RollingFileAppender">
<file value="Logs/nhibernate.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="NHibernateSQLLog" type="log4net.Appender.RollingFileAppender">
<file value="Logs/nhibernate_sql.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
Edit:
var filter = new log4net.Filter.LoggerMatchFilter();
filter.LoggerToMatch = "NHibernate.SQL";
filter.AcceptOnMatch = true;
var filterDeny = new log4net.Filter.DenyAllFilter();
var appender = new log4net.Appender.DebugAppender
{
Layout = new log4net.Layout.SimpleLayout(),
Name = "NHibernate.SQL",
Threshold = log4net.Core.Level.Debug
};
appender.AddFilter(filter);
appender.AddFilter(filterDeny);
log4net.Config.BasicConfigurator.Configure(appender);
The very best tool for the task is the NHibernate Profiler ( http://nhprof.com/ ). It's a commercial application but you get 30 days free trial.
精彩评论