log4net configuration with [assembly:]

倾然丶 夕夏残阳落幕 提交于 2021-02-18 06:30:33

问题


I was curious how the following line works for configuring log4net in an assembly:

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

I'm guessing this gets called sometime before the runtime invokes "main()" but when does this occur, and what are the implications? Are there other frameworks/libraries that use this assembly attribute for loading an initial context like this? Are there any advantages/disadvantages for doing something like this, as opposed to calling a "Configure" method in main()?


回答1:


The advantages of doing this are that the code is initialised in advance of your main code and in advance of the static initialisation.

This means that you can, for example, use log4net logging within a static-constructor. Without a way to pre-initialise log4net, in the static constructor you'd never know for certain that the code has been initialised correctly.

This area doesn't seem to be very well documented (or easy to find anyway) but I assume that the initialisation of called methods is performed at Assembly-load time.




回答2:


"I'm guessing this gets called sometime before the runtime invokes "main()" but when does this occur, and what are the implications?"


As per log4net documentation:

Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes.

So I guess you still have to call either XmlConfigurator.Configure() method or LogManager.getLogger().



来源:https://stackoverflow.com/questions/648813/log4net-configuration-with-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!