Log4Net IConfigurationReader not working

166 Views Asked by At

Here is my scenario I need to load up an different app.config file That contains my settings for log4net. In my project I have created a class that inherits from the IConfigurationReader. For the Methode getSection I load up my config file and return the section.

I can see that it is loading up the config file and the section but it is not casting it to the Type I have defined in the config file.

<common>
<logging>
  <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net4">
    <arg key="configType" value="FILE-WATCH"/>
    <arg key="configFile" value="C:\Users\dwarner\Documents\Visual Studio 2012\Projects\ConsoleApplication2\ConsoleApplication2\Config\log4net.config" />
  </factoryAdapter>
</logging>

I can see that it load up the raw XML but I have no idea why it will not create my Factory Adapter. I have tried the fully qualified name in the config with no luck.

Thanks for the Help

1

There are 1 best solutions below

0
On

As long as you set the ConfigurationReader on your LogManager and null the Adapter so it reloads the config settings it should work. You'll also need to make sure that your IConfigurationReader loads the section as it's own DocumentElement:

public class CustomAppConfigurationReader : IConfigurationReader
{
    public object GetSection(string sectionName)
    {
        var handler = new ConfigurationSectionHandler();
        var xml = LoadConfigData().GetSection(sectionName).SectionInformation.GetRawXml();
        var doc = new XmlDocument();
        doc.LoadXml(xml);
        return handler.Create(null, null, doc.DocumentElement);

    }

    /// <summary>
    /// Load configuration data from custom configuration file.
    /// </summary>
    public static Configuration LoadConfigData()
    {
        var filePath = "C:\\SomeOtheApp.config";
        var map = new ExeConfigurationFileMap {ExeConfigFilename = filePath};
        var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        return config;
    }
}