Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:59:42+00:00 2026-05-31T00:59:42+00:00

I currently have the log4net config in the applications’ app.config file, as such: …

  • 0

I currently have the log4net config in the applications’ app.config file, as such:

...
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\Service.log"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyyMMdd"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
    </appender>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
    </appender>
    <logger name="Data.WebService">
      <level value="ALL"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </logger>
    <logger name="Data.Host.HostService">
      <level value="ALL"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </logger>
  </log4net>

I know I can read this in via log4net.Config.XmlConfigurator.Configure();, however, I would like to be able to update it via some sort of call as well. I’ll be accepting configuration from a web service and, once I’ve set the new config (currently only log level, but I’m not precluding other things being configurable down the road), I need to update what is in the config file.

Having all of the configs in one file is very convenient, however, I’m open to locating the config in another file if that makes it simpler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:59:43+00:00Added an answer on May 31, 2026 at 12:59 am

    Since there is no official method to do so, I wound up writing a method that uses xpath to locate the element(s) to change and then update accordingly. Works well enough for what I need to do and is more elegant than a brute-force “readinthefiletoastringthenreplacethetextthensavethestringtothefile” approach.

        public enum Log4NetConfigItem
        {
            LOGLEVEL
        }
    
        public const string LOG4NET_CONFIGFILE = "log4net.config";
    
        public void UpdateConfiguration(Log4NetConfigItem which, object value)
        {
            // Load the config file.
            XmlDocument doc = new XmlDocument();
            doc.Load(LOG4NET_CONFIGFILE);
            // Create an XPath navigator for the document.
            XPathNavigator nav = doc.CreateNavigator();
    
            try
            {
                XPathExpression expr;
    
                // Compile the correct XPath expression for the element we want to configure.
                switch (which)
                {
                    default:
                    case Log4NetConfigItem.LOGLEVEL:
                        // Compile a standard XPath expression
                        expr = nav.Compile("/configuration/log4net/logger/level");
                        break;
                }
    
                // Locate the node(s) defined by the XPath expression.
                XPathNodeIterator iterator = nav.Select(expr);
    
                // Iterate on the node set
                while (iterator.MoveNext())
                {
                    XPathNavigator nav2 = iterator.Current.Clone();
    
                    // Update the element as required.
                    switch (which)
                    {
                        default:
                        case Log4NetConfigItem.LOGLEVEL:
                            // Update the 'value' attribute for the log level.
                            SetAttribute(nav2, String.Empty, "value", nav.NamespaceURI, value.ToString());
                            break;
                    }
                }
    
                // Save the modified config file.
                doc.Save(LOG4NET_CONFIGFILE);
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex.Message);
            }
        }
    
        void SetAttribute(System.Xml.XPath.XPathNavigator navigator, String prefix, String localName, String namespaceURI, String value)
        {
            if (navigator.CanEdit == true)
            {
                // Check if given localName exist
                if (navigator.MoveToAttribute(localName, namespaceURI))
                {
                    // Exist, so set current attribute with new value.
                    navigator.SetValue(value);
                    // Move navigator back to beginning of node
                    navigator.MoveToParent();
                }
                else
                {
                    // Does not exist, create the new attribute
                    navigator.CreateAttribute(prefix, localName, namespaceURI, value);
                }
            }
        }
    

    Note: The SetAttribute code I got from here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following log4net configuration <log4net debug=true> <appender name=RollingLogFileAppender type=log4net.Appender.RollingFileAppender> <file value=Logs\\CurrentLog.txt/> <appendToFile
Currently I have the function CreateLog() for creating a a log4net Log with name
I am learning log4net and currently testing out how to use App.Config for XMLConfiguration.
I currently have a form with inputs and name attributes. I'm able to get
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I'm trying to get logger instance by name. Currently I have a legacy project
The way I currently handle this is by having multiple config files such as:
I have integrated log4net in my app. I have a few helper methods to
I would like to set the log file name for a log4j and log4net
Currently have a drop down menu that is activated on a hover (from display:none

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.