Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Properly Configure Ninject.Extensions.Logging.Log4Net in my MVC3 project

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.

like image 610
bigmac Avatar asked Mar 20 '12 17:03

bigmac


1 Answers

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

like image 115
Remo Gloor Avatar answered Sep 23 '22 15:09

Remo Gloor