Freeman
Freeman

Reputation: 5801

How does a log4net instance get found after instantiating?

I am using log4net in a mvc4 application (for academic purposes). I am calling this method in global.asax in the Application_Start() method.

log4net.Config.XmlConfigurator.Configure();

Then in any controller of my app i can call this method:

 ILog _logger = LogManager.GetLogger(typeof(T));

The question is since when i am calling the Configure() method in the Application_Start() method, but i am not storing a reference to it in any variable, how does the GetLogger() method know where to look, more precisely where does it find the log4net instance?

Personally, I can't imagine a path to it (except maybe if i knew its memory location where it got loaded).

Upvotes: 3

Views: 1438

Answers (1)

dgvid
dgvid

Reputation: 26633

Log4net stores a reference to it in a static variable. The GetLogger method has access to that variable and returns the appropriate value. (Actually, I'm oversimplifying this a bit. See the log4net source code for full details.)

This is a variation of the singleton pattern.

By the way, you can get log4net to create more than one logger instance (as I say, it's a variation on singleton). To do so, configure the additional logger(s) via the logger element, then pass the name you've assigned via the element's name attribute to LogManager.GetLogger(). The GetLogger method will use the name passed to it as a lookup key for the appropriate static logger instance and create that instance when you first ask for it.

Upvotes: 3

Related Questions