12

I am trying to properly use Ninject to inject log4net logging into my MVC3 application. I am using the Ninject.MVC3 package, so I have the NinjectMVC3 class that automatically extends the App_Start method and contains the RegisterServices method that binds all dependencies. I also have the Ninject.Extensions.Logging.Log4Net package, but I don't know how to use it. I already know how to configure log4net in my web.config, but don't know how to use this extension for DI.

I have read all the following articles/posts, but none of them seem to define how to properly setup a project for DI logging.

At http://dotnetdarren.wordpress.com/2010/07/29/logging-in-mvc-part-4-log4net/, Darren provides a great article, but doesn't seem to deal with DI (at least I don't see it).

At Using Ninject to fill Log4Net Dependency, Remo Gloor states here that the extensions should provide all that's needed for implementation, but it doesn't show the code of how to instantiate it.

The documentation for ninject.extensions.logging at https://github.com/ninject/ninject.extensions.logging/wiki/Using is very limited at best. I have re-read it many times, and still don't see how to use bind the injection in the NinjectMVC3 class, or concrete examples of how to call the logger from my controller class for example.

At the most promising article, Moosaka provides some great code at Ninject.Extensions.Logging.Log4net unexpected behavior, but when I try it, I get a compile error in the LoggerFactory at ILogger logger = new Logger(type); stating "Cannot access protected constructor 'Logger' here". Also, he states to "Tuck this whole mess away into a separate class library". Does that mean as a whole separate project?

I'm just getting lost in all the differing options and dated posts and would like any input on how to use Dependancy Injection with Ninject and Log4Net in my MVC3 project. Also, if it matters, all of my Ninject code is in my domain project, but the logging needs done from both the domain and web project (and mocked in my unit tests). Any help is appreciated.

Community
  • 1
  • 1
bigmac
  • 2,553
  • 6
  • 38
  • 61

1 Answers1

8

You shouldn't have to configure anything except the normal log4net config.

All you have to do is to inject a ILogger wherever you want to log. https://github.com/ninject/ninject.extensions.logging/wiki/Using

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • 1
    Thanks Remo. I didn't realize that it didn't need any additional code to inject the logger. Do I still need to call `log4net.Config.XmlConfigurator.Configure();` in my global.asax.cs file? Regardless, I am running into issues in the upgrade to Ninject 3.0.0 which appears to be required. I posted my issue on your page a http://www.planetgeek.ch/2011/12/30/new-features-and-changes-of-ninject-3-0/comment-page-1/#comment-2120. If you can respond to that, I would greatly appreciate it! – bigmac Mar 21 '12 at 17:46
  • 5
    The logging works. Thanks a ton Remo! For others, you do still have to call the Configure method in global.asax.cs, but no other code is needed. However, be aware that you will need to upgrade to Ninject 3.0.0 and that might cause other side effects (like it did me) to things like Ninject.Extensions.Conventions. – bigmac Mar 21 '12 at 22:50
  • 1
    This was surprisingly difficult trying to get log4net working in an old Ninject enabled project - I had to uninstall all logging support (Ninject.Extensions.Logging.Log4net, Ninject.Extensions.Logging, then finally log4net) , then reinstall everything using *Ninject.Extensions.Logging.Log4net* Nuget package as a base in order to avoid "Could not load log4net assembly..." errors. – Coruscate5 Apr 27 '18 at 01:38
  • What I understand is, if I use Ninject.Extensions.Logging, I have to have this `using Ninject.Extensions.Logging;` in all the classes that I want to use logging in? Is that right? My concern is, can I still use a wrapper for my logger? I don't want my classes to know which logger I am using. And @Coruscate5, you are absolutely right. It is a nightmare making the two work together. – Sнаđошƒаӽ Sep 22 '19 at 06:42
  • @Remo Can you please add more details how to configure this whole thing? The link you added doesn't show much. – Sнаđошƒаӽ Sep 22 '19 at 06:43