예제 #1
0
        /// <summary>
        /// Inits the plug-in with configured factory data.
        /// </summary>
        /// <param name="factoryData">Retrieved factory settings.
        /// This parameter is null if no configuration at all
        /// was found.</param>
        public void Init(string factoryData)
        {
            String loggerTypeName;

            try
            {
                // load the factoryData XML
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(factoryData);

                // obtain the logger element and inspect the 'type' attribute
                XmlElement   loggerElement       = (XmlElement)xmlDoc.GetElementsByTagName("logger")[0];
                XmlAttribute loggerTypeAttribute = loggerElement.Attributes[0];
                loggerTypeName = loggerTypeAttribute.Value;
            }
            catch (Exception e)
            {
                DiagnosticLogger.Error(e, "An exception was thrown while trying to parse the given XML configuration [{0}]",
                                       factoryData);
                return;
            }

            ILogger logger = ActivatorUtils.Instantiate <ILogger>(loggerTypeName, DiagnosticLogger);

            if (logger != null)
            {
                Logger = logger;
            }
        }
예제 #2
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);
        }
        /// <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="NullLoggerFactory"/> 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, DiagnosticLogger);

            //if the factory is configurable, invoke its Init method
            IConfigurableLoggerFactory cf = factory as IConfigurableLoggerFactory;

            if (cf != null)
            {
                cf.Init(factoryConfiguration.FactoryData);
            }

            if (factory == null)
            {
                factory = NullLoggerFactory.Instance;
            }


            return(factory);
        }