Dynamically update log4net configuration section in web.config file

31 Views Asked by At

I've been looking into this for a few hours already, and it seems like it can't be done, but I was thinking of trying here as well.

For consistency purposes, I want to create a method that updates some values in the log4net appender section of the web.config file, in a .Net Framework 4.6 application. Ideally using a configuration object

Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

This is something I implemented easily for appSettings, connectionStrings, even system.serviceModel section.

It seems like log4net section does not have ConfigurationSection object that I could use in the same way as for the others, as it wasn't developed. I did find how to load custom configuration dynamically for log4net, even how to update a file used only by log4net, but not how to update the values in web.config file, which is what I would ideally want, for consistency

Could this be done? Thank you!

1

There are 1 best solutions below

0
Ileana Profeanu On

Ultimately, I did use this solution, which updates web.config file so application will restart (I implement an additional flag to check whether values need updating)

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"));