Empty NHibernate log file with log4net - nhibernate

I'm trying to get logging enabled on my application using Fluent NHibernate and log4net. I have tried the things described here, here, here and here. The log file is getting created, but nothing is getting written to it. The other log files for this and other applications all seam to be working OK, so I'm assuming the issue is something with my configuration.
Here is the code I have put in place to try to get this working:
The section of the config file with all of my log4net settings relevant to this APP:
<appender name="RollingFileAppenderNHibernate"
type="log4net.Appender.RollingFileAppender">
<file value="C:\temp\RollingLogFileNHibernate" />
<appendToFile value="true" />
<ImmediateFlush value="true" />
<rollingStyle value="Date" />
<DatePattern value="yyyyMMdd.\l\o\g" />
<StaticLogFileName value="false" />
<MaxSizeRollBackups value="1" />
<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="ALL"/>
<appender-ref ref="RollingFileAppenderNHibernate"/>
</logger>
My NHibernate configuration:
public static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(OracleClientConfiguration.Oracle10
.ConnectionString((conn =>
conn.FromConnectionStringWithKey("APPDB")))
.ShowSql())
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<{ObjectName}>())
.BuildSessionFactory();
}
In the Application_Start() of my Global.asax.cs:
log4net.Config.XmlConfigurator.Configure();
I have tried several different variations of these settings but the result is always the same, an empty log file.

If the XML is a true repesentation of your config, I can see two potential problems:
There is no '>' at the end of the appender opening tag.
There is > at the end of the layout closing tag instead of '>'.
Of course, these may have just been caused by the copy and paste into this post.

It turns out that the problem had to do with the overly complicated and convoluted way that logging was handled in our framework code that we have wrapped around log4net. Once I figured out the correct hoops to jump through the logging started working correctly.

Related

how to show generated sql query by fluent nhibernate

I am using fluent NHibernate. I want to see sql query which is generated by fluent NHibernate. Is there any way to get this query at runtime.
Generated SQL could be observed in two ways. We can use log4net and output it into any kind of appender (e.g. file) like this:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\Logs\NH_SQL.log" />
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="[%-3d|%-4t|%-5p|%-75c] %m | %-10u %n" />
</layout>
<!-- example with file up to 1MB migrated into max 2 backup files -->
<appendToFile value="true" />
<maximumFileSize value="1MB" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
</appender>
<logger name="NHibernate.SQL">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</logger>
Or we can use the setting show_sql documented here
3.5. Optional configuration properties
Which could be turned on like this:
Fluently
.Configure()
.Database( MsSqlConfiguration
.MsSql2008
.ShowSql()
...
And then all SQL statements will be written into console.

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.

Vb.net Visual Studio 2008 log4net multiple modules, same app.config different loggers, dont work

I have 2 console apps projects in the same directory but different projects. There is some common code in the App_Code directory and a common app.config which gets build into seperate .exe.config files.
One module (VScanDemonStarter) starts up and writes to one logger with its own appender going to a seperate file. It uses an process.start() to execute the other module (VScanDemon) in another command prompt hidden window.
When I run VScanDemon by itself it puts entries into its log file. When I run VScanDemonStarter it puts entries into its (different) log file, the VScanDemon log file gets created, but no entries. I can see it is executing because some files get moved from one directory to another. Just no Log entries.
the config looks like
<root>
<level value="INFO" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log/vscandemonstarter.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd hh:mm:ss} - %m%n" />
</layout>
</appender>
<appender name="vsdemonlogfileappender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log/vscandemon.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd hh:mm:ss} - %m%n" />
</layout>
</appender>
and the code bodies set up and call the bodies with.
VScanDemonStarter:
top of module:
Private ReadOnly log As ILog = log4net.LogManager.GetLogger("default")
top of main:
log4net.Config.XmlConfigurator.Configure()
example calls:
If log.IsInfoEnabled Then log.Info("VScanDemonStarter:Main: ----called----")
VBScanDemon:
top of module:
Private ReadOnly log As ILog = log4net.LogManager.GetLogger("VSDemonLogger")
top of main:
log4net.Config.XmlConfigurator.Configure()
VBScanDemon:
If log.IsInfoEnabled Then log.Info("VScanDemon:Main: ----called----")
I don't get any log entries from VScanDemon.
Sorry left out the top of the configuration
<log4net debug="true">
<logger name="default">
<level value="INFO"/>
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="VSDemonLogger">
<level value="INFO"/>
<appender-ref ref="vsdemonlogfileappender" />
</logger>
I have one app.config but two projects in the same directory using it. They generate seperate configs from that one source .exe.config
I looked at the console output from VScanDemon and it looks like it is picking up the configuration with no issues. But still an empty log. I don't know if I need to add a flush or something.
Are you useing the same log4net configuration for both modules?
If not, are they really different at runtime?
I think "default" should return the Root-logger and "VSDemonLogger" the other one but that one is not included in your posted configuration.
EDIT:
AFAIK all logger in the configuration will be "created", which results in the generated log files for your VSDemonLogger and VScanDemonStarter even if you do not use them.
You are using relativ pathes in the configuration, are they still valid for VSDemonLogger if you call it from VScanDemonStarter?

NHibernate Enabling log4net

I have enabled log4net and run my app which is giving an exception.
But the log file is empty.
Doesn't NHibernate log info about the exception???
Malcolm
You need to configure log4net. Just by adding log4net dll to the project doesn't log anything. You need to create appenders to specify where all the loggin should be directed to. Create a xml file like this one:
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Trace.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="30" />
<maximumFileSize value="1000KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level - %message%newline" />
</layout>
<threshold value="DEBUG"/>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
...and configure it when starting up the application:
public static void Main()
{
var logconfig = new System.IO.FileInfo(PATH_TO_LOG_CONFIG);
if(logconfig.Exists)
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(logconfig);
}
}

How do I configure log4net so that log.IsDebugEnabled is true?

I am trying to use log4net in an ASP.NET application with Visual Studio 2005. I have declared an instance of the logger like so:
Private Shared ReadOnly log As ILog = LogManager.GetLogger("")
I am trying to use it in the following manner:
If log.IsDebugEnabled Then
log.Debug("Integration Services Constructed")
End If
Here is my configuration:
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\\logs\\logfile.log"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
</log4net>
Unfortunately, log.IsDebugEnabled is always false. How do I configure log4net so that I can log only debug messages?
Before calling LogManager.GetLogger("")
You have to call log4net.Config.XmlConfigurator.Configure();
In an ASP.NET app you probably want to put this call in Application_Start
Yes, do it like Anson said. Also, if you are calling Configure in a class library you can do that by adding an attribute to your class:
[assembly: XmlConfigurator(Watch = true)]
and if you're using log4net.config file, use it like that instead:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
If you are using a separate configuration file for log4net, do this: after following all the other setup instructions, make sure that u right click on the file in the visual studio solution explorer, select properties, expand the "Advanced" option group, set the "Copy To Output Directory" value as "Copy always". That will do the magic... :) cheers!!
If you are setting log4net up in code rather than in a config file, you can call log4net.Config.BasicConfigurator.Configure before GetLogger.
Use this in any method before you use log :
log4net.Config.XmlConfigurator.Configure();
In App.Config ,the settings should be :
<root>
<level value="ALL" />
<appender-ref ref="AppenderName" />
</root>
VB.NET -
<Assembly: log4net.Config.XmlConfigurator(Watch:=True)>