コード例 #1
0
ファイル: LogManager.cs プロジェクト: RoboterYang/ECode
        private static IAppender CreateAppender(AppenderInfo appenderInfo)
        {
            try
            {
                var appenderType = TypeResolutionUtil.ResolveType(appenderInfo.TypeClass);
                var appender     = (IAppender)Activator.CreateInstance(appenderType);
                appender.Initialize(appenderInfo.Options);

                return(appender);
            }
            catch (Exception ex)
            {
                LogLog.Error($"Logging config: cannot create appender '{appenderInfo.TypeClass}'.", ex);
                return(null);
            }
        }
コード例 #2
0
ファイル: LogManager.cs プロジェクト: RoboterYang/ECode
        private static void LoadConfig()
        {
            try
            {
                var defaultLevel    = Level.ALL;
                var defaultLevelStr = ConfigurationManager.Get(CONFIG_NAMESPACE_PREFIX + "level");
                if (string.IsNullOrWhiteSpace(defaultLevelStr))
                {
                    LogLog.Warn("Logging config: 'level' not exists.");
                }
                else
                {
                    if (Enum.TryParse(typeof(Level), defaultLevelStr, out object resolvedLevel))
                    {
                        defaultLevel = (Level)resolvedLevel;
                    }
                    else
                    {
                        LogLog.Warn($"Logging config: cannot parse '{defaultLevelStr}' to '{typeof(Level)}'.");
                    }
                }

                var defaultAppenderRefs   = new string[0];
                var defaultAppenderRefStr = ConfigurationManager.Get(CONFIG_NAMESPACE_PREFIX + "default");
                if (string.IsNullOrWhiteSpace(defaultAppenderRefStr))
                {
                    LogLog.Warn("Logging config: 'default' not exists.");
                }
                else
                {
                    defaultAppenderRefs = JsonUtil.Deserialize <string[]>(defaultAppenderRefStr);
                }

                var namedLoggers    = new Dictionary <string, LoggerInfo>(StringComparer.InvariantCultureIgnoreCase);
                var namedLoggersStr = ConfigurationManager.Get(CONFIG_NAMESPACE_PREFIX + "loggers");
                if (string.IsNullOrWhiteSpace(namedLoggersStr))
                {
                    LogLog.Warn("Logging config: 'loggers' not exists.");
                }
                else
                {
                    var loggerInfos = JsonUtil.Deserialize <List <LoggerInfo> >(namedLoggersStr);
                    foreach (var loggerInfo in loggerInfos)
                    {
                        if (string.IsNullOrWhiteSpace(loggerInfo.Name))
                        {
                            LogLog.Warn("Logging config: logger name cannot be null or empty.");
                            continue;
                        }

                        loggerInfo.Name = loggerInfo.Name.Trim();

                        if (loggerInfo.Appenders == null)
                        {
                            loggerInfo.Appenders = new List <string>();
                        }

                        namedLoggers[loggerInfo.Name] = loggerInfo;
                    }
                }

                var    namedAppenders    = new Dictionary <string, AppenderInfo>(StringComparer.InvariantCultureIgnoreCase);
                string namedAppendersStr = ConfigurationManager.Get(CONFIG_NAMESPACE_PREFIX + "appenders");
                if (string.IsNullOrWhiteSpace(namedAppendersStr))
                {
                    LogLog.Warn("Logging config: 'appenders' not exists.");
                }
                else
                {
                    var appenderInfos = JsonUtil.Deserialize <List <dynamic> >(namedAppendersStr);
                    foreach (var appenderInfo in appenderInfos)
                    {
                        string name    = string.Empty;
                        string type    = string.Empty;
                        var    options = new NameValueCollection();

                        foreach (var keyValue in appenderInfo)
                        {
                            if (keyValue.Name == "name")
                            {
                                name = (keyValue.Value ?? string.Empty).ToString();
                            }
                            else if (keyValue.Name == "type")
                            {
                                type = (keyValue.Value ?? string.Empty).ToString();
                            }
                            else
                            {
                                options[keyValue.Name] = (keyValue.Value ?? string.Empty).ToString();
                            }
                        }

                        if (string.IsNullOrWhiteSpace(name))
                        {
                            LogLog.Warn("Logging config: appender name cannot be null or empty.");
                            continue;
                        }

                        if (string.IsNullOrWhiteSpace(type))
                        {
                            LogLog.Warn("Logging config: appender type cannot be null or empty.");
                            continue;
                        }

                        namedAppenders[name] = new AppenderInfo()
                        {
                            Name = name, TypeClass = type, Options = options
                        };
                    }
                }


                var parsedAppenders   = new Dictionary <string, IAppender>(StringComparer.InvariantCultureIgnoreCase);
                var requiredAppenders = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);
                foreach (string appender in defaultAppenderRefs)
                {
                    if (string.IsNullOrWhiteSpace(appender))
                    {
                        continue;
                    }

                    requiredAppenders.Add(appender.Trim());
                }

                foreach (var loggerInfo in namedLoggers.Values)
                {
                    foreach (string appender in loggerInfo.Appenders)
                    {
                        if (string.IsNullOrWhiteSpace(appender))
                        {
                            continue;
                        }

                        requiredAppenders.Add(appender.Trim());
                    }
                }

                foreach (string appenderName in requiredAppenders)
                {
                    if (!namedAppenders.TryGetValue(appenderName, out AppenderInfo appenderInfo))
                    {
                        continue;
                    }

                    var appender = CreateAppender(appenderInfo);
                    if (appender != null)
                    {
                        parsedAppenders[appenderInfo.Name] = appender;
                    }
                }

                LoadConfigSync(defaultLevel, defaultAppenderRefs, namedLoggers, parsedAppenders);
            }
            catch (Exception ex)
            { LogLog.Error($"Load log config error.", ex); }
        }