Exemple #1
0
        /// <summary>
        /// Creates a logger by name
        /// </summary>
        /// <param name="collectorName">Name of collector</param>
        /// <returns></returns>
        /// <remarks>There are special well-known loggers:
        /// <list>
        /// <item><code>Logbus</code><description>Collector that forwards messages to the current Logbus instance</description></item>
        /// </list></remarks>
        /// <exception cref="InvalidOperationException">No or invalid configuration is specified</exception>
        public static ILogCollector CreateCollector(string collectorName)
        {
            if (string.IsNullOrEmpty(collectorName))
            {
                throw new ArgumentNullException("collectorName");
            }

            if (Configuration == null || Configuration.collector == null || Configuration.collector.Length < 1)
            {
                throw new InvalidOperationException(
                          "Invalid configuration. Either no configuration is set or no collector is specified");
            }

            //Try to find the first logger marked default
            foreach (LogbusCollectorDefinition def in Configuration.collector)
            {
                if (def.id == collectorName)
                {
                    return(CreateCollector(def));
                }
            }

            //Let's see if the collector name is well-known
            try
            {
                WellKnownLogger knownLogger = (WellKnownLogger)Enum.Parse(typeof(WellKnownLogger), collectorName);
                switch (knownLogger)
                {
                case WellKnownLogger.Logbus:
                {
                    return(LogbusSingletonHelper.Instance);
                }

                case WellKnownLogger.CollectorInternal:
                {
                    return(new ConsoleCollector());
                }

                case WellKnownLogger.Client:
                {
                    return(new ConsoleCollector());
                }
                }
            }
            catch (ArgumentException)
            {
            }

            //Else throw error: collector is not defined in configuration
            throw new LogbusException(string.Format("Collector {0} not found", collectorName));
        }
Exemple #2
0
        /// <summary>
        /// Returns the collector associated to the given logger.
        /// It's different from obtaining a collector from its name
        /// </summary>
        /// <param name="loggerName">Name of logger</param>
        /// <returns></returns>
        public static ILogCollector CreateCollectorForLogger(string loggerName)
        {
            if (Configuration != null && Configuration.logger != null && Configuration.logger.Length > 0)
            {
                foreach (LoggerDefinition def in Configuration.logger)
                {
                    if (def.name == loggerName)
                    {
                        return(CreateCollector(def.collectorid));
                    }
                }
            }

            //Let's see if the collector name is well-known
            try
            {
                WellKnownLogger knownLogger = (WellKnownLogger)Enum.Parse(typeof(WellKnownLogger), loggerName);
                switch (knownLogger)
                {
                case WellKnownLogger.Logbus:
                {
                    return(LogbusSingletonHelper.Instance);
                }

                case WellKnownLogger.CollectorInternal:
                {
                    return(new ConsoleCollector());
                }

                case WellKnownLogger.Client:
                {
                    return(new ConsoleCollector());
                }
                }
            }
            catch (ArgumentException)
            {
            }

            throw new LogbusConfigurationException(string.Format("Collector {0} not found", loggerName));
        }
Exemple #3
0
 /// <summary>
 /// Gets a well-known logger
 /// </summary>
 /// <param name="knownLogger">Logger to retrieve</param>
 /// <returns></returns>
 public static ILog GetLogger(WellKnownLogger knownLogger)
 {
     return(GetLogger(Enum.GetName(typeof(WellKnownLogger), knownLogger)));
 }
Exemple #4
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);
            }
        }
Exemple #5
0
 /// <summary>
 /// Creates collector for well known logger
 /// </summary>
 /// <param name="knownLogger">Well-known default logger</param>
 /// <returns></returns>
 public static ILogCollector CreateCollector(WellKnownLogger knownLogger)
 {
     return(CreateCollector(Enum.GetName(typeof(WellKnownLogger), knownLogger)));
 }