1

I have a console application project that is using Ninject and Log4Net.

When i run the app on my machine, the logging is working fine. When i run the app on the production server, the logging is working fine. When i run the program via TaskScheduller task which is being set so that is is being run by some other user, i get no logging output by any of the appenders. I'm using RollingFileAppender, SmtpAppender and AdoNetAppender. The strange thing is, that the program is running fine, it just doesnt log anything.

I presume that because the app is working if i run it locally, the log4net configuration is fine.

I resolve logger in the main method of the program and then inject it via constructor parameter when needed. This is how i get the logger in the main method of the app:

XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));

var kernel = new StandardKernel();
var loggerFactory = kernel.Get<Log4NetLoggerFactory>();
ILogger logger = loggerFactory.GetCurrentClassLogger();

logger.Info(" Test ");

Any hints, pointers or anything....as i don't know what else to try.

Alex Dee
  • 129
  • 2
  • 7

1 Answers1

0

The extension is normally used like this:

public class MyClass
{
    private readonly ILogger log;

    public MyClass(ILogger log)
    {
        this.log = log;
        log.Info("Created MyClass");
    }
}

XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
using (IKernel kernel = new StandardKernel())
{
    kernel.Bind<MyClass>().ToSelf();
    kernel.Get<MyClass>(); // will cause the log message to print
}

Just let Ninject worry about injecting the ILogger into your class. You can't request an ILogger from the IKernel in the same place you declare the kernel because you've got no context.

You can do this though:

ILogger log = new Log4NetLoggerFactory().GetCurrentClassLogger();
log.Info("Test");
Adam Rodger
  • 3,472
  • 4
  • 33
  • 45
  • I have changed it to the way you wrote, but i still don't get any logging output when i run the app with task scheduler. I have turned on the Internal debug of log4net and if i run the app with my user, i get a bunch of additional text displayed. But if i run the app via task scheduler, i get no errors in event viewer, and no log output. I can see that the app is running in windows task manager. – Alex Dee Mar 07 '13 at 13:51