Beispiel #1
0
        private static ConsoleStore ParseConsoleStoreSettings()
        {
            var stores = jsonDoc.SelectToken(nameof(Config.stores));

            var console = stores.SelectToken(ConsoleSM.NAME);

            return(new ConsoleStore
            {
                minLogLevel = console.Value <string>(nameof(ConsoleStore.minLogLevel)).StringToEnum <LogLevel>(),
                outputPattern = StringStoreManager.GetOutputActions(console.Value <string>(nameof(ConsoleStore.outputPattern)))
            });
        }
Beispiel #2
0
        private static FileStore ParseFileStoreSettings()
        {
            var stores = jsonDoc.SelectToken(nameof(Config.stores));

            var file      = stores.SelectToken(FileSM.NAME);
            var fileStore = new FileStore
            {
                path                   = file.Value <string>(nameof(FileStore.path)),
                minLogLevel            = file.Value <string>(nameof(FileStore.minLogLevel)).StringToEnum <LogLevel>(),
                outputPattern          = StringStoreManager.GetOutputActions(file.Value <string>(nameof(FileStore.outputPattern))),
                recreationPeriodInDays = file.Value <int>(nameof(FileStore.recreationPeriodInDays))
            };

            if (fileStore.recreationPeriodInDays <= 0 || fileStore.recreationPeriodInDays > 365)
            {
                throw new StruLogConfigException($"{nameof(fileStore.recreationPeriodInDays)} must be in interval [1,365]");
            }
            return(fileStore);
        }
Beispiel #3
0
        /// <summary>
        /// Парсинг из конфига, временное прослушивание телеграм-бота, запись настроек в конфиг
        /// </summary>
        /// <returns></returns>
        private static TelegramStore DefineTelegramStoreSettings()
        {
            var stores = jsonDoc.SelectToken(nameof(Config.stores));

            var telegramFileConfig  = stores.SelectToken(TelegramSM.NAME);
            var telegramStoreConfig = new TelegramStore
            {
                minLogLevel   = telegramFileConfig.Value <string>(nameof(Store.minLogLevel)).StringToEnum <LogLevel>(),
                outputPattern = StringStoreManager.GetOutputActions(telegramFileConfig.Value <string>(nameof(TelegramStore.outputPattern))),
                token         = telegramFileConfig.Value <string>(nameof(TelegramStore.token)),
            };

            int sendingPeriod = telegramFileConfig.Value <int>(nameof(TelegramStore.sendingPeriod));

            if (sendingPeriod < 1000)
            {
                Console.WriteLine(@"StruLog: telegram/sendingPeriod (see config) must be >= 1000, will be 3000");
                telegramStoreConfig.sendingPeriod = 3000;
            }
            else
            {
                telegramStoreConfig.sendingPeriod = sendingPeriod;
            }

            var intensivity = telegramFileConfig.SelectToken(nameof(TelegramStore.intensivity));

            if (intensivity?.HasValues ?? false)
            {
                telegramStoreConfig.intensivity = new TelegramStore.IntensivityControl
                {
                    enable             = intensivity.Value <bool>(nameof(TelegramStore.intensivity.enable)),
                    nMessagesPerPeriod = intensivity.Value <long>(nameof(TelegramStore.intensivity.nMessagesPerPeriod)),
                    period             = TimeSpan.Parse(intensivity.Value <string>(nameof(TelegramStore.intensivity.period)))
                }
            }
            ;
            else
            {
                telegramStoreConfig.intensivity = new TelegramStore.IntensivityControl {
                    enable = false
                }
            };

            var            chatIdsFromConfig = telegramFileConfig.SelectToken("chats");
            HashSet <long> chatIds;

            if (chatIdsFromConfig.HasValues)
            {
                chatIds = NewtonsoftJsonTools.ConvertJTokenToIEnumerable <long>(chatIdsFromConfig).ToHashSet();
            }
            else
            {
                chatIds = new HashSet <long>();
            }

            if (chatIds.Count == 0 || (options?.AddTelegramConsumers ?? false))
            {
                DefineTelegramChatId(telegramFileConfig, telegramStoreConfig, chatIds);
                SaveChangesToConfig();
            }
            else
            {
                telegramStoreConfig.chatIds = chatIds.ToImmutableList();
            }

            return(telegramStoreConfig);
        }