Trouble enabling log4net in Fluent NHibernate - nhibernate

I need help in configuring log4net and Fluent NHibernate. It seems log4net is starting, but I'm not getting any information from NHibernate/Fluent NHibernate. I want this information to debug faulty mapping.
Configuration in App.config
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<logger name="NHibernate"
additivity="false">
<level value="ALL"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
Here's how I launch log4net and configuration.
log4net.Config.XmlConfigurator.Configure();
FluentConfiguration cfg = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.ShowSql().InMemory())
.Diagnostics(x => x.Enable());
DbCtx.Source = new SessionSource(cfg.BuildConfiguration()
.Properties, new PSModel());
The exception I'm getting is in the last line on cfg.BuildConfiguration(). I don't get any entries in log on how mapping went, though.
Thank you in advance for your help.

If you are wanting to see the messages in the output window in visual studio you should use the following instead:
<appender name="ConsoleAppender" type="log4net.Appender.TraceAppender" >
Also to see the log messages you may want to throw everything NHibernate spits out into your LogFileAppender as well:
<logger name="NHibernate"
additivity="false">
<level value="ALL"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="LogFileAppender"/>
</logger>

The problem laid in the fact that exception occured in Fluent NHibernate therefore logging for NHibernate wasn't able to kick in.

Related

Configure Nhibernate log into file

I have programmatically create log4net log from config file:
var properties = new NameValueCollection
{
{"configType", "FILE"},
{"configFile", #"c:/log4net.config"}
};
Common.Logging.LogManager.Adapter = new Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter(properties);
This is my log4net.config (EDITED for simplicity as dove suggested) :
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="c:/log.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingFile" />
</root>
<logger name="NHibernate">
<level value="All" />
<appender-ref ref="RollingFile" />
</logger>
</log4net>
I got Spring.NET successfuly logging into that file, but not NHibernate.
Nhibernate is configured fluenltly:
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
var msSqlCfg = MsSqlConfiguration.MsSql2000.ConnectionString(ConnectionString)
.ShowSql();
Fluently.Configure(config).Database(msSqlCfg)
.Mappings(m => m.FluentMappings.Add<EmployeeMap>())
// Other mappings
.BuildSessionFactory();
}
What should I fix to get Nhibernate log working?
Does it log if you generate statistics?
Configuration.ExposeConfiguration(c =>
c.SetProperty("generate_statistics", "true"));
To narrow things down can you remove additivity flag and appeder from within logger and just log one to start, i.e. exactly like this
<logger name="NHibernate">
<level value="ALL" />
</logger>

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, Settings.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.

Nhibernate show_sql=false not working

This is how I create Session Factory using Fluent Nhibernate
public static ISessionFactory CreateSessionFactory()
{
FluentNHibernate.Cfg.FluentConfiguration fconfig;
fconfig = FluentNHibernate.Cfg.Fluently.Configure()
.Cache(c=>c.UseQueryCache())
.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("Database"))
.ProxyFactoryFactory<NHibernate.ByteCode.LinFu.ProxyFactoryFactory>()
.Mappings(
m => m.FluentMappings
.AddFromAssemblyOf<NHibernateRepositoryRegistry>()
.Conventions.Add<Conventions.PrimaryKeyConvention>()
.Conventions.Add<Conventions.IdForeignKeyConvention>()
.Conventions.Add<Conventions.ReferenceForeignKeyConstraintNameConvention>()
)
// i have tried this but it did not work
//.ExposeConfiguration(config=>{config.SetProperty("show_sql", "false");});
return fconfig.BuildSessionFactory();
}
This is my log4net config inside Web.config
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="nhibernate.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFile2" type="log4net.Appender.RollingFileAppender">
<file value="nhibernatesql.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="NHibernate">
<level value="INFO" />
<appender-ref ref="RollingFile" />
</logger>
<logger name="NHibernate.SQL">
<level value="ALL" />
<appender-ref ref="RollingFile2" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
I had ShowSql() setup perviously
and I was trying to set it via web.config appsettings so i can turn on or off based on configuration
so I removed ShowSql() and tried following ( right now i am just passing "false" value )
.ExposeConfiguration(config=>{config.SetProperty("show_sql", "false");});
also tried
.ExposeConfiguration(config=>{config.SetProperty("hibernate.show_sql", "false");});
but I am still getting sql generated in nhibernatesql.log file
I am using NHibernate v3.1.0.4000 and FluentNHibernate v1.2.0.712 and ASP.NET MVC3
does anyone know why sql is still generating?
does show_sql or ShowSql() is only meant for Console.output purpose and sql is sent to log4net regardless ?
my main concern is slow performance if ShowSQL is set to true
not sure whether I am setting ShowSql somewhere else
ok I think I got the answer
based on http://www.davesquared.net/2008/01/viewing-sql-generated-by-nhibernate.html
show_sql is used of Console.out purpose only and sql is sent to log4net regardless.. and that is why I am seeing sql in the logs

How to suppress logging message for nHibernate

I am using log4Net in my application which also uses nHibernate. Now my eventlogger is flooded with nHibernate messages which I want to suppress.
I have tried this but it did not work.
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="LogName" value="App Log" />
<param name="ApplicationName" value="DataService" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="NHibernate*" />
<acceptOnMatch value="false" />
</filter>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="EventLogAppender"/>
</root>
</log4net>
You can add a logger for nHibernate and set up a different level:
<logger name="NHibernate" additivity="false">
<level value="WARN"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
you should use
<loggerToMatch value="NHibernate.*" /> or <loggerToMatch value="NHibernate" />
not
<loggerToMatch value="NHibernate*" />
you can find details in regex rules

Can't get log4net to output anything when using NHibernate

I have this in my web.config file (edited to reflect some changes):
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net debug="true">
<appender name="DebugAppender" type="log4net.Appender.AspNetTraceAppender">
<layout type="log4net.Layout.PatternLayout, log4net">
<param name="ConversionPattern" value="%d %p %m%n" />
</layout>
</appender>
<appender name="DataLog" type="log4net.Appender.RollingFileAppender">
<file value="c:\temp\nhlog.txt" />
<appendToFile value="true" />
<maximumFileSize value="100MB" />
<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>
<root>
<priority value="DEBUG" />
<appender-ref ref="DebugAppender" />
</root>
<logger name="NHibernate" additivity="false">
<level value="INFO"/>
<appender-ref ref="DebugAppender"/>
</logger>
</log4net>
I'm calling log4net.Config.XmlConfigurator.Configure() from Application_Start().
I have [assembly: log4net.Config.XmlConfigurator(Watch = true)] in the AssemblyInfo.cs file in my web project.
Why do I get no NHibernate debugging messages in my output window? (I trying to get ALL of the messages that NHibernate spits out.) If I use the RollingFileAppender it works fine. I've tried the DebugAppender, TraceAppender, OutputDebugStringAppender, and AspNetTraceAppender and none of them output stuff to the output window.
Try using a different appender.
ConsoleAppenders probably don't work for web applications. Use something like a FileAppender.
See this for examples on configuraiton.
Similar to quip's answer, ConsoleAppender is not the right appender to use in a web application. An OutputDebugStringAppender will give you messages in your debug window. You may also want to look into AspNetTraceAppender.
You could try to associate the specific NHibernate logger with a log4net appender.
If you want to log all the generated sql, try adding this to your log4net config section:
<logger name="NHibernate.SQL" additivity="false">
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</logger>
If, for some reason, you only wanted to log Select statements and diagnostic info, you could use NHibernate.Loader.Loader, like this:
<logger name="NHibernate.Loader.Loader" additivity="false">
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</logger>