How to log to a file from an Eclipse RCP application? - eclipse-plugin

I can't log to a file from my eclipse rcp application.
So, I followed the guide on https://www.vogella.com/tutorials/EclipseLogging/article.html.
I installed Logback SLF4J Binding, logback classic module and logback core module from orbit repository.
I created a plugin that read the config file logback.xml. That plugin require ch.qos.logback.classic & ch.qos.logback.core. I also imported org.sl4j.
I imported org.slf4j on the other plugins.
Here is the logback.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<property name="HOME_LOG" value="d://logFiles/app.log"/>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${HOME_LOG}</file>
<encoder>
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -FILE- %msg%n
</Pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -OUT- %msg%n
</Pattern>
</layout>
</appender>
<logger name="fr.test.sirius.logger" level="debug"
additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE"/>
</logger>
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE"/>
</root>
</configuration>
When I launch my Eclipse RCP application with "run as" everything is working as attended.
When I create an update site and install the feature on Eclipse, the log file is not created. I tried to launch eclipse with -console argument, the logs are printed correctly in the OSGi console.
edit: The logs are not printed correctly in the OSGI console, seems like an issue with the activator.
Thanks

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

Another log4net, fluent nhibernate configuration issue

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.

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.

How do I use the log4net FileAppender under mono when running as a background process?

I'm having trouble using a log4net FileAppender with mono. If I spawn the processess in the background (i.e. mono MyApp.exe &), the process suspends after a short period of time. If I run the process in the foreground, the logger works correctly.
On the other hand, if I use a ConsoleAppender and redirect it (i.e. mono MyApp.exe > debug.log &), things are fine.
Here is my configuration for the application:
<appender name="debug-log" type="log4net.Appender.FileAppender">
<file value="debug.log" />
<appendToFile value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-8timestamp [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-8timestamp [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="debug-log" />
</root>
I am using the mono-specific version of the log4net assembly.
EDIT
Mono version: 2.10.1
I just noticed another interesting behavior... I am able to use the FileAppender to write to the debug-log as long as I redirect stdout anywhere. For example, if I start my process using the following:
mono MyApp.exe > /dev/null &
The process executes normally, writing to the debug-log. In this case, I am only using the debug-log, not the console appender. Using the same configuration, the process will suspend after writing a small amount of data to debug-log when I remove the redirect.

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>