Another log4net, fluent nhibernate configuration issue - nhibernate

This is driving me crazy... I've spent half a day trying to turn everything aroung but I can't get log4net to spit out anything about NHibernate.
Here's my current Fluent NHibernate config:
var configuration = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile(AppConstants.PATH_FILENAME_DB))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<...>())
[...Mappings...]
.Diagnostics(x => x.Enable());
My app.config:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="data/log/unprocessed.log"/>
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<logger name="NHibernate.SQL" additivity="false">
<level value="ALL"/>
<appender-ref ref="LogFileAppender"/>
</logger>
<root>
<level value="INFO"/>
<appender-ref ref="LogFileAppender"/>
</root>
</log4net>
I can log stuff right after I initialize the main form:
log4net.Config.XmlConfigurator.Configure();
logger.Info("Here is a debug log!!");
And it works 100%. However I'm not able to get anything out of Nhibernate.
I've tried:
setting ShowSql() in my Fluent NHibernate config => SQL appears in VS console.
adding NHibernate logger
removing additivity
using ConsoleAppender instead of LogFileAppender (still no SQL in the output though I get regular logs from the application)
recompiling Fluent NHibernate, tried 3 different versions of log4net...
My Fluent Nhibernate mappings are good, my app is working perfectly without log4net.
I still am unable to log any queries from NHibernate... Not sure what is going on but if you have a clue I'll be happy to try it out!!!
Thanks!

Well I ended up finding the issue...
All my DLLs were located in /lib folder instead of the output folder.
Part of my app.config had this:
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib"/>
<dependentAssembly>
<assemblyIdentity name="System.Data" publicKeyToken="b77a5c561934e089"/>
<bindingRedirect oldVersion="2.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
Putting log4net.dll in my output folder fixed the issue.

Related

log4net set log file at a relative path

In my vb.net project, I use log4net and use an absolute path for the log file. But in the end I need to write log file at C:\Users\UserName\AppData\Local\Temp\Log folder (the username is a variable). Can't find a way how to set this in the log4net file.
My log4net file currently like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="ExampleAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Log\App.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="_yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level -
[%logger] %message%newline" />
</layout>
</appender>
<logger name="App">
<level value="ALL" />
<appender-ref ref="ExampleAppender" />
</logger>
</log4net>
</configuration>
Is there a way to perform this? thanks.
You can use environment variables like this:
<file type="log4net.Util.PatternString" value="${LOCALAPPDATA}\Temp\Log\App.log" />
This creates a log file (for me) at C:\Users\stuartd\AppData\Local\Temp\Log\App.log

Trouble enabling log4net in Fluent 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.

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>

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?

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)>