3
votes

I am having issues with windsor castle log4net facility integration in ASP.Net MVC. I have two project with same code (sample I wrote to keep the reference and my current project). Sample project works fine but my current project doesn't work I get a null reference exception. I have an asp.net mvc 3 application with integration of castle windsor IOC. Added necessary dlls via NuGet. Here is my Code fragement: Step 1: Logger Installer

public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{            
     container.AddFacility<LoggingFacility>(f => f.UseLog4Net());            
     //container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithConfig("log4net.config")); 
}

Step 2:

log4net.config

Step 3: In my AccountController(LogOn Method)..

public ILogger Logger { get; set; }
Logger.InfoFormat("User {0} has successfully signed in.", model.UserName);

In past week, I have tried everything and read 20-30 posts on this website but I still cannot get it work. May be if someone can tell me what the issue is and point me to a step-by-step tutorial, I would great be thankful.

I am not sure what am i missing. I have checked Project->Properties->Application->.Net Framework 4.0 but still doesn't work.

Note: I followed the castle windsor website tutorial http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Five-Adding-logging-support.ashx

3
what happens if you make the logger a constructor dependency (or keep it a property dependency but make it a required one?)kko
I'm having a similar issue. Castle's support appears to be terribly unreliable. I have as bunch of classes that use the logger. These classes are all generated from templates, so the way they use the logger is identical in all cases, but for some classes it works and others it doesn't and it changes from run-to-run of my application. Really annoying.Pete

3 Answers

0
votes

Use Castle.Core.Logging.ILogger not the one from log4net. That's the one LoggingFacility works with.

0
votes

I think the the class that's using the ILogger must finish instantiating before you try using the ILogger.

So if you try to call ILogger before your constructor call finishes, your ILogger will be null due to the way Windsor loads log4net.

Are you calling your LogOn() method inside the constructor of AccountController?

0
votes

It is not clear from your question if you are instantiating the controllers from the Windsor container. Maybe that is something obvious and not worth mentioning, but maybe you have just forgot about it, so it is good to rule that out.