예제 #1
0
        /// <summary>
        /// Creates a factory based on a given configuration.
        /// If the factory provides invalid information, an error is logged through
        /// the internal logger, and a <see cref="NOPLoggerFactory"/> returned.
        /// </summary>
        /// <param name="factoryConfiguration">The configuration that provides type
        /// information for the <see cref="ILoggerFactory"/> that is being created.</param>
        /// <returns>Factory instance.</returns>
        private ILoggerFactory CreateFactoryInstance(FactoryConfigurationElement factoryConfiguration)
        {
            ILoggerFactory factory = ActivatorUtils.Instantiate<ILoggerFactory>(factoryConfiguration.Type);
            IConfigurableLoggerFactory cf = factory as IConfigurableLoggerFactory;

            // If the factory is configurable, invoke its Init method
            if (cf != null)
            {
                cf.Init(factoryConfiguration.FactoryData);
            }
            else
            {
                if (!string.IsNullOrEmpty(factoryConfiguration.FactoryData))
                {
                    throw new ConfigurationErrorsException("Factory " + factoryConfiguration.Type + " does not implement IConfigurableLoggerFactory.");
                }
            }

            return factory;
        }
        public void Configuration_SlfConfigurationSection_ValidFactoryTest()
        {
            Assert.IsNotNull(_configuration);
            var section = _configuration.GetSection("slf4net-valid-factory") as SlfConfigurationSection;

            Assert.IsNotNull(section);
            Assert.IsNotNull(section.Factory);

            Assert.IsFalse(string.IsNullOrEmpty(section.Factory.Type));
            Assert.AreEqual("slf4net.Factories.SimpleLoggerFactory, slf4net.Simple", section.Factory.Type);
            Assert.IsTrue(string.IsNullOrEmpty(section.Factory.FactoryData));

            string expected = "test.factory.type";
            string actual;

            section.Factory.Type = expected;
            actual = section.Factory.Type;
            Assert.AreEqual(expected, actual);

            FactoryConfigurationElement expectedFactory = new FactoryConfigurationElement();
            FactoryConfigurationElement actualFactory;

            actualFactory = section.Factory;
            Assert.AreNotEqual(expectedFactory, actualFactory);

            section.Factory = expectedFactory;
            actualFactory = section.Factory;
            Assert.AreEqual(expectedFactory, actualFactory);
        }