Change app.config at runtime

旧巷老猫 提交于 2019-12-11 07:58:20

问题


I'm currently trying to create the app.config at runtime. The basic idea is that I'm deploying several config files and based on an environment variable the application itself decides which one to use.

Currently I'm copying the config file I'd like to use to myapp.exe.config, and refresh all the sections. The problem I'm facing is, that log4net seems to mess up things, but only in release build (as the static fields are initilized at a different time).

I explicitly DON'T want to decide at build time which config to use.

Any ideas (probably there is a better approach)?

tia Martin


回答1:


You can configure log4net after you decided what config to use and copied it into place by using XmlConfigurator.Configure(new System.IO.FileInfo("Filename.config"))




回答2:


Can you do all the copying in one AppDomain when you start up - an AppDomain which doesn't use any settings - and then start the real application in a new AppDomain? Heck, as an alternative would it be possible to have separate processes for this? One bootstrap process would just make sure the right configuration is in place, then start up the real application.

In both of these solutions you would do everything you need before you really use any settings, which should keep things simpler.

Note that static initialization can be controlled to some extent by the presence or absence of a static constructor - see my article on beforefieldinit for more details. I wouldn't recommend using this to fix your current system though - it'll end up being quite fragile when it comes to maintaining the code.




回答3:


Instead of config files, couldn't you use Application Settings? If it's not something the user needs to change, you might be better off coming up with several sets of internally scoped Settings and just switching between them.



来源:https://stackoverflow.com/questions/1542171/change-app-config-at-runtime

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