Log4net configuration with [build:]

I was curious how the following line works to configure log4net in the assembly:

[assembly: log4net.Config.XmlConfigurator(Watch=true)] 

I guess this is called sometime before runtime calls "main ()", but when does this happen and what are the consequences? Are there other structures / libraries that use this assembly attribute to load an initial context like this? Are there any advantages / disadvantages to doing something similar, as opposed to calling the "Configure" method in main ()?

+6
c # configuration assemblies log4net
source share
2 answers

The benefits of this are that the code is initialized before your main code and before static initialization.

This means that you can, for example, use log4net to log in the static constructor. Without a way to pre-initialize log4net in a static constructor, you will never know for sure that the code was correctly initialized.

This area does not seem to be very well documented (or it is easy to find anyway), but I assume that the initialization of the called methods is done during Assembly loading.

+7
source share

"I suppose this is called sometime before the runtime calls" main () ", but when does it happen, and what are the consequences?"


As the log4net documentation :

Using attributes can be a clearer method of determining where the application configuration will be downloaded. However, it is worth noting that attributes are purely passive. This is information only. Therefore, if you use configuration attributes, you must call log4net so that it can read the attributes.

So, I think you still have to call the XmlConfigurator.Configure() or LogManager.getLogger() method.

0
source share

All Articles