.net dynamically refresh app.config

前端 未结 10 791
闹比i
闹比i 2020-12-02 23:28

How do I dynamically reload the app.config in a .net Windows application? I need to turn logging on and off dynamically and not just based upon the value at application sta

10条回答
  •  无人及你
    2020-12-02 23:33

    If you are using log4Net, you can do what you asked:

    Although it is possible to add your log4net configuration settings to your project’s app.config or web.config file, it is preferable to place them in a separate configuration file. Aside from the obvious benefit of maintainability, it has the added benefit that log4net can place a FileSystemWatcher object on your config file to monitor when it changes and update its settings dynamically.

    To use a separate config file, add a file named Log4Net.config to your project and add the following attribute to your AssemblyInfo.cs file:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile="Log4Net.config", Watch = true)]
    

    Note: for web applications, this assumes Log4Net.config resides in the web root. Ensure the log4net.config file is marked as “Copy To Output” -> “Copy Always” in Properties.

提交回复
热议问题