Search code examples
c#logginglog4netnancy

Logging in a console application which hosts a NancyFx web service


What I am doing
I am writing a Nancy self hosting application (runs in a console window)

My problem
I cannot get log4net to log anything to my console window. (Specifically - I cannot get this to work when hosting a NancyFx service, I have no problem using Log4Net in other console applications.)

What I have researched
I have looked at the following pages (in addition to searching StackOverflow):
http://www.philhack.com/wire-up-log4net-to-nancy/ https://lookonmyworks.co.uk/2012/03/14/logging-unhandled-exceptions-with-nancy/

My log4net Configuration

<?xml version="1.0" encoding="utf-8"?>
   <configuration>
      <configSections>
         <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      </configSections>

      <log4net>
         <appender name="Console" type="log4net.Appender.ConsoleAppender">
             <layout type="log4net.Layout.PatternLayout">
               <conversionPattern value="%date %-5level: %message%newline" />
            </layout>
         </appender>

         <root>
            <level value="DEBUG"/>
            <appender-ref ref="Console"/>
         </root>
      </log4net>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
    </startup>
 </configuration>

Example of my usage of Log4Net

 public class MyNancyModule : NancyModule
 {
    private static ILog Logger = LogManager.LogFactory.GetLogger(typeof(MyNancyModule));

    public MyNancyModule()
    {
       Logger.Debug("This is a test");
    }
 }

My problem (again)
I cannot get log4net to log anything to my console window using the above configuration. I understand from the blog posts above that Nancy does something to configure the error handling pipelines which interferes with log4Net, but I don't quite understand what it is doing or how.

How can I get log4Net working in a self hosted Nancy service in order to log to the console window (I am not particularly interested in logging to the in-built diagnostics page)?

I haven't tried adding the log manager to the IOC container as in all my other applications, I usually have a static log manager per class which gives me class specific logging - the example in the blog post will log everything as having originated from the bootstrapper class.

Edit I forgot to mention that I am using ServiceStack.Logging.Log4Net for the logManager. It also doesn't appear to work in my example if I do:

private static ILog Logger = LogManager.GetLogger(typeof(ApiReverseProxyModule));

Solution

  • Thanks to Stuartd for pointing me in the right direction. The blogs I mentioned were a bit misleading - turns out I was missing

    LogManager.LogFactory = new Log4NetFactory(configureLog4Net: true);
    

    from my main method - so I was always using a null logging factory.

    This appears to be nothing to do with NancyFX!

    Hope this helps someone!