Exemplo n.º 1
0
        /// <summary>
        /// Builds a <see cref="ILoggerFactoryAdapter"/> instance from the given <see cref="LogSetting"/>
        /// using <see cref="Activator"/>.
        /// </summary>
        /// <param name="setting"></param>
        /// <returns>the <see cref="ILoggerFactoryAdapter"/> instance. Is never <c>null</c></returns>
        private static ILoggerFactoryAdapter BuildLoggerFactoryAdapterFromLogSettings(LogSetting setting)
        {
            ArgUtils.AssertNotNull("setting", setting);
            // already ensured by LogSetting
            //            AssertArgIsAssignable<ILoggerFactoryAdapter>("setting.FactoryAdapterType", setting.FactoryAdapterType
            //                                , "Specified FactoryAdapter does not implement {0}.  Check implementation of class {1}"
            //                                , typeof(ILoggerFactoryAdapter).FullName
            //                                , setting.FactoryAdapterType.AssemblyQualifiedName);

            ILoggerFactoryAdapter adapter = null;

            ArgUtils.Guard(delegate
            {
                if (setting.Properties != null &&
                    setting.Properties.Count > 0)
                {
                    object[] args = { setting.Properties };

                    adapter = (ILoggerFactoryAdapter)Activator.CreateInstance(setting.FactoryAdapterType, args);
                }
                else
                {
                    adapter = (ILoggerFactoryAdapter)Activator.CreateInstance(setting.FactoryAdapterType);
                }
            }
                           , "Unable to create instance of type {0}. Possible explanation is lack of zero arg and single arg NameValueCollection constructors"
                           , setting.FactoryAdapterType.FullName
                           );

            // make sure
            ArgUtils.AssertNotNull("adapter", adapter, "Activator.CreateInstance() returned <null>");
            return(adapter);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Builds the logger factory adapter.
        /// </summary>
        /// <returns>a factory adapter instance. Is never <c>null</c>.</returns>
        private static ILoggerFactoryAdapter BuildLoggerFactoryAdapter()
        {
#if UNITY3D
            ILoggerFactoryAdapter defaultFactory = new UnityDebugLoggerFactoryAdapter();
            return(defaultFactory);
#else
            object sectionResult = null;

            ArgUtils.Guard(delegate
            {
                sectionResult = ConfigurationReader.GetSection(COMMON_LOGGING_SECTION);
            }
                           , "Failed obtaining configuration for Common.Logging from configuration section 'common/logging'.");

            // configuration reader returned <null>
            if (sectionResult == null)
            {
                string message = (ConfigurationReader.GetType() == typeof(DefaultConfigurationReader))
                                     ? string.Format("no configuration section <{0}> found - suppressing logging output", COMMON_LOGGING_SECTION)
                                     : string.Format("Custom ConfigurationReader '{0}' returned <null> - suppressing logging output", ConfigurationReader.GetType().FullName);
#if PORTABLE
                Debug.WriteLine(message);
#else
                Trace.WriteLine(message);
#endif

                ILoggerFactoryAdapter defaultFactory = new NoOpLoggerFactoryAdapter();
                return(defaultFactory);
            }

            // ready to use ILoggerFactoryAdapter?
            if (sectionResult is ILoggerFactoryAdapter)
            {
#if PORTABLE
                Debug.WriteLine(string.Format("Using ILoggerFactoryAdapter returned from custom ConfigurationReader '{0}'", ConfigurationReader.GetType().FullName));
#else
                Trace.WriteLine(string.Format("Using ILoggerFactoryAdapter returned from custom ConfigurationReader '{0}'", ConfigurationReader.GetType().FullName));
#endif
                return((ILoggerFactoryAdapter)sectionResult);
            }

            // ensure what's left is a LogSetting instance
            ArgUtils.Guard(delegate
            {
                ArgUtils.AssertIsAssignable <LogSetting>("sectionResult", sectionResult.GetType());
            }
                           , "ConfigurationReader {0} returned unknown settings instance of type {1}"
                           , ConfigurationReader.GetType().FullName, sectionResult.GetType().FullName);

            ILoggerFactoryAdapter adapter = null;
            ArgUtils.Guard(delegate
            {
                adapter = BuildLoggerFactoryAdapterFromLogSettings((LogSetting)sectionResult);
            }
                           , "Failed creating LoggerFactoryAdapter from settings");

            return(adapter);
#endif
        }