Пример #1
0
        /// <summary>
        /// Creates collector from configuration definition
        /// </summary>
        /// <param name="def">Configuration entry of collector</param>
        /// <returns></returns>
        internal static ILogCollector CreateCollector(LogCollectorDefinitionBase def)
        {
            if (def == null)
            {
                throw new ArgumentNullException("def");
            }
            if (string.IsNullOrEmpty(def.type))
            {
                throw new ArgumentException("Configuration entry doesn't specified required collector type", "def");
            }
            try
            {
                string typename = def.type;
                if (typename.IndexOf('.') < 0)
                {
                    //This is probably a plain class name, overriding to It.Unina.Dis.Logbus.InChannels namespace
                    const string namespc      = "It.Unina.Dis.Logbus.Collectors";
                    string       assemblyname = typeof(CollectorHelper).Assembly.GetName().ToString();
                    typename = string.Format("{0}.{1}, {2}", namespc, typename, assemblyname);
                }
                Type loggerType = Type.GetType(typename);
                if (!typeof(ILogCollector).IsAssignableFrom(loggerType))
                {
                    LogbusConfigurationException ex =
                        new LogbusConfigurationException("Registered collector type does not implement ILogCollector");
                    ex.Data.Add("type", loggerType);
                    throw ex;
                }
                ILogCollector ret = Activator.CreateInstance(loggerType) as ILogCollector;
                if (def.param != null && ret is IConfigurable)
                {
                    foreach (KeyValuePair kvp in def.param)
                    {
                        ((IConfigurable)ret).SetConfigurationParameter(kvp.name, kvp.value);
                    }
                }

                return(ret);
            }
            catch (Exception ex)
            {
                throw new LogbusConfigurationException("Invalid collector configuration", ex);
            }
        }
Пример #2
0
        /// <summary>
        /// Creates default logger by reflection
        /// </summary>
        /// <returns></returns>
        private static ILogger InstantiateLogger()
        {
            try
            {
                Type loggerType = typeof(SimpleLogImpl);
                if (Configuration != null)
                {
                    if (!string.IsNullOrEmpty(Configuration.defaultloggertype))
                    {
                        string typename = Configuration.defaultloggertype;

                        if (typename.IndexOf('.') < 0)
                        {
                            //This is probably a plain class name, overriding to It.Unina.Dis.Logbus.Loggers namespace
                            const string namespc      = "It.Unina.Dis.Logbus.Loggers";
                            string       assemblyname = typeof(LoggerHelper).Assembly.GetName().ToString();
                            typename = string.Format("{0}.{1}, {2}", namespc, typename, assemblyname);
                        }
                        loggerType = Type.GetType(typename);
                        if (!typeof(ILogger).IsAssignableFrom(loggerType))
                        {
                            LogbusConfigurationException ex =
                                new LogbusConfigurationException(
                                    "Registered logger type does not implement ILog");
                            ex.Data.Add("type", loggerType);
                            throw ex;
                        }
                    }
                }
                ILogger ret = Activator.CreateInstance(loggerType) as ILogger;
                return(ret);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Unable to create logger because of configuration error", ex);
            }
        }
Пример #3
0
        /// <summary>
        /// Creates logger by reflection for configured logger
        /// </summary>
        /// <returns></returns>
        private static ILogger InstantiateLogger(string loggerName)
        {
            if (string.IsNullOrEmpty(loggerName))
            {
                throw new ArgumentNullException("loggerName");
            }
            if (_loggers.ContainsKey(loggerName))
            {
                return(_loggers[loggerName]);
            }

            bool permanent = false;

            try
            {
                Type loggerType = typeof(SimpleLogImpl);
                if (Configuration != null)
                {
                    string typename = Configuration.defaultloggertype;

                    try
                    {
                        LoggerDefinition def = GetDefinition(loggerName);
                        if (!string.IsNullOrEmpty(def.type))
                        {
                            typename = def.type;
                        }
                        permanent = def.permanent;
                    }
                    catch (LogbusException)
                    {
                    }
                    catch (InvalidOperationException)
                    {
                    }

                    if (!string.IsNullOrEmpty(typename))
                    {
                        if (typename.IndexOf('.') < 0)
                        {
                            //This is probably a plain class name, overriding to It.Unina.Dis.Logbus.Loggers namespace
                            const string namespc      = "It.Unina.Dis.Logbus.Loggers";
                            string       assemblyname = typeof(LoggerHelper).Assembly.GetName().ToString();
                            typename = string.Format("{0}.{1}, {2}", namespc, typename, assemblyname);
                        }
                        loggerType = Type.GetType(typename);
                        if (!typeof(ILogger).IsAssignableFrom(loggerType))
                        {
                            LogbusConfigurationException ex =
                                new LogbusConfigurationException(
                                    "Registered logger type does not implement ILogger");
                            ex.Data.Add("type", loggerType);
                            throw ex;
                        }
                    }
                }
                ILogger ret = (ILogger)Activator.CreateInstance(loggerType);
                ret.LogName = loggerName;

                //Let's see if the collector name is well-known
                try
                {
                    WellKnownLogger knownLogger = (WellKnownLogger)Enum.Parse(typeof(WellKnownLogger), loggerName);
                    ret.HeartbeatInterval = 0;
                    switch (knownLogger)
                    {
                    case WellKnownLogger.Logbus:
                    {
                        ret.Facility = SyslogFacility.Internally;
                        break;
                    }

                    case WellKnownLogger.CollectorInternal:
                    {
                        ret.Facility = SyslogFacility.Internally;
                        break;
                    }

                    case WellKnownLogger.Client:
                    {
                        ret.Facility = SyslogFacility.Local5;
                        break;
                    }
                    }
                }
                catch (ArgumentException)
                {
                }

                if (permanent)
                {
                    _loggers.Add(loggerName, ret);
                }
                return(ret);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Unable to create logger because of configuration error", ex);
            }
        }