我已经研究了几个小时了,似乎无法完成,但我也想在这里尝试一下。
为了保持一致性,我想创建一个方法来更新 .Net Framework 4.6 应用程序中 web.config 文件的 log4net Appender 部分中的某些值。理想情况下使用配置对象
Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
这是我为 appSettings、connectionStrings、甚至 system.serviceModel 部分轻松实现的东西。
似乎 log4net 部分没有 ConfigurationSection 对象,我可以像其他部分一样使用它,因为它尚未开发。我确实找到了如何动态加载 log4net 的自定义配置,甚至如何更新仅由 log4net 使用的文件,但没有找到如何更新 web.config 文件中的值,这正是我理想想要的一致性
这可以做到吗?谢谢!
最终,我确实使用了这个解决方案,它更新了 web.config 文件,以便应用程序将重新启动(我实现了一个附加标志来检查值是否需要更新)
HttpContext context = HttpContext.Current;
XmlDocument doc = new XmlDocument();
doc.Load(context.Server.MapPath("~/Web.config"));
XmlNode xx = doc.SelectSingleNode("/configuration/log4net/appender/@type");
xx.Value = "my_value";
doc.Save(context.Server.MapPath("~/Web.config"));