C# 如何以编程方式更新log4net配置文件?

C# 如何以编程方式更新log4net配置文件?,c#,.net,log4net,C#,.net,Log4net,我目前在应用程序的app.config文件中有log4net配置,如下所示: 。。。 (目前只有日志级别,但我不排除以后可以配置其他东西),我需要更新配置文件中的内容 将所有配置放在一个文件中非常方便,但是,如果这样做更简单的话,我愿意将配置放在另一个文件中。这基本上就是log4net的配置-不幸的是,它还没有解决。因为没有正式的方法来解决这个问题,所以我编写了一个使用xpath来定位元素的方法更改,然后相应地更新。对于我需要做的事情来说,它足够好了,而且比蛮力“读入文件以字符串形式,然后替换文

我目前在应用程序的app.config文件中有log4net配置,如下所示:

。。。
(目前只有日志级别,但我不排除以后可以配置其他东西),我需要更新配置文件中的内容


将所有配置放在一个文件中非常方便,但是,如果这样做更简单的话,我愿意将配置放在另一个文件中。

这基本上就是log4net的配置-不幸的是,它还没有解决。

因为没有正式的方法来解决这个问题,所以我编写了一个使用xpath来定位元素的方法更改,然后相应地更新。对于我需要做的事情来说,它足够好了,而且比蛮力“读入文件以字符串形式,然后替换文本,然后保存字符串以文件形式”的方法更优雅

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

注意:我从中获得的SetAttribute代码。

嗯,这是一个打击。那么还有其他建议吗?仍然需要完成。最简单的方法可能是拥有一个log4netConfigurationChange对象,该对象支持可序列化的配置更改。反序列化时,它可能有一个重新应用log4net更改的函数。太棒了!这就是我要找的。