/// <summary>
        /// Builds the logger factory adapter.
        /// </summary>
        /// <returns></returns>
        private static ILoggerFactoryAdapter BuildLoggerFactoryAdapter()
        {
            LogSetting setting = null;

            try
            {
                setting = (LogSetting)ConfigurationManager.GetSection(IBATIS_SECTION_LOGGING);
            }
            catch (Exception ex)
            {
                ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                ILog log = defaultFactory.GetLogger(typeof(LogManager));
                log.Warn("Unable to read configuration. Using default logger.", ex);
                return(defaultFactory);
            }

            if (setting != null && !typeof(ILoggerFactoryAdapter).IsAssignableFrom(setting.FactoryAdapterType))
            {
                ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                ILog log = defaultFactory.GetLogger(typeof(LogManager));
                log.Warn("Type " + setting.FactoryAdapterType.FullName + " does not implement ILoggerFactoryAdapter. Using default logger");
                return(defaultFactory);
            }

            ILoggerFactoryAdapter instance = null;

            if (setting != null)
            {
                if (setting.Properties.Count > 0)
                {
                    try
                    {
                        object[] args = { setting.Properties };

                        instance = (ILoggerFactoryAdapter)Activator.CreateInstance(setting.FactoryAdapterType, args);
                    }
                    catch (Exception ex)
                    {
                        ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                        ILog log = defaultFactory.GetLogger(typeof(LogManager));
                        log.Warn("Unable to create instance of type " + setting.FactoryAdapterType.FullName + ". Using default logger.", ex);
                        return(defaultFactory);
                    }
                }
                else
                {
                    try
                    {
                        instance = (ILoggerFactoryAdapter)Activator.CreateInstance(setting.FactoryAdapterType);
                    }
                    catch (Exception ex)
                    {
                        ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                        ILog log = defaultFactory.GetLogger(typeof(LogManager));
                        log.Warn("Unable to create instance of type " + setting.FactoryAdapterType.FullName + ". Using default logger.", ex);
                        return(defaultFactory);
                    }
                }
            }
            else
            {
                ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                ILog log = defaultFactory.GetLogger(typeof(LogManager));
                log.Warn("Unable to read configuration IBatisNet/logging. Using default logger.");
                return(defaultFactory);
            }

            return(instance);
        }
Beispiel #2
0
        /// <summary>
        /// Builds the logger factory adapter.
        /// </summary>
        /// <returns></returns>
        private static ILoggerFactoryAdapter BuildLoggerFactoryAdapter()
        {
            LogSetting        setting = null;
            IList <Exception> errors  = new List <Exception>();

            string[] probeSectionNames = new[] { MYBATIS_SECTION_LOGGING, IBATIS_SECTION_LOGGING };
            foreach (var probeSectionName in probeSectionNames)
            {
                try
                {
                    setting = (LogSetting)ConfigurationManager.GetSection(probeSectionName);
                    if (setting != null)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    errors.Add(ex);
                }
            }

            if (errors.Count > 0 || setting == null)
            {
                ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                ILog log = defaultFactory.GetLogger(typeof(LogManager));
                foreach (var ex in errors)
                {
                    log.Warn("Unable to read configuration.", ex);
                }
                log.Warn(string.Format("Unable to read configuration from {0}. Using default logger.",
                                       string.Join(", ", probeSectionNames)));

                return(defaultFactory);
            }

            if (!typeof(ILoggerFactoryAdapter).IsAssignableFrom(setting.FactoryAdapterType))
            {
                ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                ILog log = defaultFactory.GetLogger(typeof(LogManager));
                log.Warn("Type " + setting.FactoryAdapterType.FullName +
                         " does not implement ILoggerFactoryAdapter. Using default logger");
                return(defaultFactory);
            }

            ILoggerFactoryAdapter instance = null;

            if (setting.Properties.Count > 0)
            {
                try
                {
                    object[] args = { setting.Properties };

                    instance = (ILoggerFactoryAdapter)Activator.CreateInstance(setting.FactoryAdapterType, args);
                }
                catch (Exception ex)
                {
                    ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                    ILog log = defaultFactory.GetLogger(typeof(LogManager));
                    log.Warn(
                        "Unable to create instance of type " + setting.FactoryAdapterType.FullName +
                        ". Using default logger.", ex);
                    return(defaultFactory);
                }
            }
            else
            {
                try
                {
                    instance = (ILoggerFactoryAdapter)Activator.CreateInstance(setting.FactoryAdapterType);
                }
                catch (Exception ex)
                {
                    ILoggerFactoryAdapter defaultFactory = BuildDefaultLoggerFactoryAdapter();
                    ILog log = defaultFactory.GetLogger(typeof(LogManager));
                    log.Warn(
                        "Unable to create instance of type " + setting.FactoryAdapterType.FullName +
                        ". Using default logger.", ex);
                    return(defaultFactory);
                }
            }

            return(instance);
        }