Ejemplo n.º 1
0
        public async IAsyncEnumerable <Nation> GetRecruitableNationsAsync(int number, bool isAPI, EventId id)
        {
            _logger.LogInformation(id, LogMessageBuilder.Build(id, $"{number} recruitable nations requested"));
            var pendingCount = NationManager.GetNationCountByStatusName("pending");

            _logger.LogInformation(id, LogMessageBuilder.Build(id, $"Pending: {pendingCount}"));
            if (number > pendingCount)
            {
                number = pendingCount;
            }
            for (int i = 0; i < number; i++)
            {
                if (currentRNStatus != null && currentRNStatus.CurrentCount > currentRNStatus.FinalCount)
                {
                    break;
                }
                if (IsReceivingRecruitableNations && !isAPI)
                {
                    currentRNStatus.CurrentCount++;
                }
                var nation = await GetRecruitableNationAsync(id, isAPI);

                if (nation == null)
                {
                    break;
                }
                yield return(nation);
            }
        }
Ejemplo n.º 2
0
        public async Task UpdateRecruitmentStatsAsync()
        {
            try
            {
                _logger.LogInformation(_defaulEventId, LogMessageBuilder.Build(_defaulEventId, "Updating Recruitment Stats"));
                var today = DateTime.Today.Date;

                var sent   = NationManager.GetNationsByStatusName("send").Select(n => n.Name).ToList();
                var manual = NationManager.GetNationsByStatusName("reserved_manual").Select(n => n.Name).ToList();
                var region = await _dumpDataService.GetRegionAsync(BaseApiService.ToID(_config.NationStatesRegionName));

                var apiRecruited    = region.NATIONS.Where(n => sent.Any(s => n.NAME == s)).Select(n => n.NAME).ToList();
                var manualRecruited = region.NATIONS.Where(n => manual.Any(m => n.NAME == m)).Select(n => n.NAME).ToList();

                RStatDbUpdate();

                ApiRecruited    = apiRecruited.Count;
                ApiRatio        = Math.Round((100 * ApiRecruited / (sent.Count + ApiFailed + 0.0)), 2);
                ManualReserved  = manual.Count;
                ManualRecruited = manualRecruited.Count;
                ManualRatio     = Math.Round((100 * ManualRecruited / (manual.Count + 0.0)), 2);
                _logger.LogInformation(_defaulEventId, LogMessageBuilder.Build(_defaulEventId, "Recruitment Stats Updated"));
            }
            catch (Exception ex)
            {
                _logger.LogCritical(_defaulEventId, ex, LogMessageBuilder.Build(_defaulEventId, "A critical error occured."));
            }
        }
        private Task DiscordClient_Log(LogMessage arg)
        {
            var    id      = LogEventIdProvider.GetEventIdByType(LoggingEvent.DiscordLogEvent);
            string message = LogMessageBuilder.Build(id, $"[{arg.Source}] {arg.Message}");

            switch (arg.Severity)
            {
            case LogSeverity.Critical:
                _logger.LogCritical(id, message);
                break;

            case LogSeverity.Error:
                _logger.LogError(id, message);
                break;

            case LogSeverity.Warning:
                _logger.LogWarning(id, message);
                break;

            case LogSeverity.Info:
                _logger.LogInformation(id, message);
                break;

            default:
                _logger.LogDebug(id, $"Severity: {arg.Severity.ToString()} {message}");
                break;
            }
            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
        private async Task <List <REGION> > GetRegionToRecruitFromAsync(EventId id)
        {
            List <REGION> regionsToRecruitFrom = new List <REGION>();
            var           regionNames          = _config.RegionsToRecruitFrom.Split(";");

            foreach (var regionName in regionNames)
            {
                if (!string.IsNullOrWhiteSpace(regionName))
                {
                    var region = await _dumpDataService.GetRegionAsync(BaseApiService.ToID(regionName));

                    if (region != null)
                    {
                        regionsToRecruitFrom.Add(region);
                        _logger.LogInformation(id, LogMessageBuilder.Build(id, $"Region '{regionName}' added to regionsToRecruitFrom."));
                    }
                    else
                    {
                        _logger.LogWarning(id, LogMessageBuilder.Build(id, $"Region for name '{regionName}' couldn't be found in dumps."));
                    }
                }
            }

            return(regionsToRecruitFrom);
        }
Ejemplo n.º 5
0
        public async Task <HttpResponseMessage> ExecuteRequest(HttpRequestMessage httpRequest, EventId eventId)
        {
            if (httpRequest is null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }
            if (string.IsNullOrWhiteSpace(_config.Contact))
            {
                throw new InvalidOperationException("No Request can be send when contact info hasn't been provided.");
            }
            using (HttpClient client = GetHttpClient())
            {
                client.AddCyborgianStatesUserAgent(AppSettings.VERSION, _config.Contact);
                _logger.LogDebug(eventId, LogMessageBuilder.Build(eventId, $"Executing {httpRequest.Method}-Request to {httpRequest.RequestUri}"));
                HttpResponseMessage response = await client.SendAsync(httpRequest).ConfigureAwait(false);

                if (!response.IsSuccessStatusCode)
                {
                    _logger.LogError(eventId, LogMessageBuilder.Build(eventId, $"Request finished with response: {(int)response.StatusCode}: {response.ReasonPhrase}"));
                }
                else
                {
                    _logger.LogDebug(eventId, LogMessageBuilder.Build(eventId, $"Request finished with response: {(int)response.StatusCode}: {response.ReasonPhrase}"));
                }
                return(response);
            }
        }
Ejemplo n.º 6
0
        public async Task <GZipStream> GetNationStatesDumpStream(NationStatesDumpType type)
        {
            var eventId = LogEventIdProvider.GetRandomLogEventId();

            try
            {
                string url = "https://www.nationstates.net/pages/";
                if (type == NationStatesDumpType.Nations)
                {
                    url += "nations.xml.gz";
                    _logger.LogInformation(eventId, LogMessageBuilder.Build(eventId, "Retrieval of latest Nation dump requested"));
                }
                else if (type == NationStatesDumpType.Regions)
                {
                    url += "regions.xml.gz";
                    _logger.LogInformation(eventId, LogMessageBuilder.Build(eventId, "Retrieval of latest Region dump requested"));
                }
                else
                {
                    throw new NotImplementedException($"Retrieval for DumpType {type} not implemented yet.");
                }
                var stream = await ExecuteRequestWithStreamResult(url, eventId);

                var compressed = new GZipStream(stream, CompressionMode.Decompress);
                return(compressed);
            }
            finally
            {
                LogEventIdProvider.ReleaseEventId(eventId);
            }
        }
Ejemplo n.º 7
0
        private async Task <bool> IsNationRecruitableAsync(Nation nation, EventId id)
        {
            if (nation != null)
            {
                var criteriaFit = await DoesNationFitCriteriaAsync(nation.Name);

                if (criteriaFit)
                {
                    var apiResponse = await WouldReceiveTelegramAsync(nation, id);

                    if (apiResponse == 0)
                    {
                        _logger.LogDebug(id, LogMessageBuilder.Build(id, $"{nation.Name} - No receive."));
                    }
                    else if (apiResponse == 1)
                    {
                        return(true);
                    }
                    else
                    {
                        _logger.LogWarning(id, LogMessageBuilder.Build(id, $"Recruitable nation check: {nation.Name} failed."));
                        await NationManager.SetNationStatusToAsync(nation, "failed");

                        return(false);
                    }
                }
                else
                {
                    _logger.LogDebug(id, LogMessageBuilder.Build(id, $"{nation.Name} does not fit criteria and is therefore skipped."));
                }
            }
            await NationManager.SetNationStatusToAsync(nation, "skipped");

            return(false);
        }
        private void StateAndExceptionNullReturnsNull()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions();
            var builder         = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, null, null);
        }
Ejemplo n.º 9
0
        public async Task GetBasicStats(params string[] args)
        {
            var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.GetRegionStats);

            try
            {
                string regionName = string.Join(" ", args);
                _logger.LogInformation(id, LogMessageBuilder.Build(id, $"BasicRegionStats for {regionName} requested."));
                XmlDocument regionStats = await dataService.GetRegionStatsAsync(regionName, id);

                if (regionStats != null)
                {
                    var name       = regionStats.GetElementsByTagName("NAME")[0].InnerText;
                    var numnations = regionStats.GetElementsByTagName("NUMNATIONS")[0].InnerText;
                    var wadelegate = regionStats.GetElementsByTagName("DELEGATE")[0].InnerText;
                    var founder    = regionStats.GetElementsByTagName("FOUNDER")[0].InnerText;
                    var founded    = regionStats.GetElementsByTagName("FOUNDED")[0].InnerText;
                    var flagUrl    = regionStats.GetElementsByTagName("FLAG")[0].InnerText;
                    var power      = regionStats.GetElementsByTagName("POWER")[0].InnerText;
                    var tags       = regionStats.GetElementsByTagName("TAGS")[0].ChildNodes;
                    var tagList    = "";
                    for (int i = 0; i < tags.Count; i++)
                    {
                        tagList += BaseApiService.FromID(tags.Item(i).InnerText) + ", ";
                    }
                    tagList = tagList.Remove(tagList.Length - 2);
                    var regionUrl = $"https://www.nationstates.net/region={BaseApiService.ToID(regionName)}";



                    var builder = new EmbedBuilder();
                    builder.WithThumbnailUrl(flagUrl);
                    builder.WithTitle($"BasicStats for Region");
                    builder.WithDescription($"**[{name}]({regionUrl})** {Environment.NewLine}" +
                                            $"[{numnations} nations]({regionUrl}/page=list_nations) | {founded} | Power: {power}");
                    builder.AddField("Founder", $"[{await GetFullNationName(founder, id)}](https://www.nationstates.net/nation={BaseApiService.ToID(founder)})");
                    builder.AddField("Delegate", await GetDelegateNationString(wadelegate, id));
                    builder.WithFooter($"NationStatesApiBot {AppSettings.VERSION} by drehtisch");
                    builder.WithColor(new Color(_rnd.Next(0, 256), _rnd.Next(0, 256), _rnd.Next(0, 256)));
                    await ReplyAsync(embed : builder.Build());
                }
                else
                {
                    var builder = new EmbedBuilder();
                    builder.WithTitle($"Something went wrong.");
                    builder.WithDescription("Probably no such region.");
                    await ReplyAsync(embed : builder.Build());
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical(id, ex, LogMessageBuilder.Build(id, "A critical error occured."));
                await ReplyAsync("Something went wrong. Sorry :(");
            }
        }
Ejemplo n.º 10
0
        public void TestLogMessageBuilder()
        {
            var res = Helpers.GetEventIdByType(Enums.LoggingEvent.GetNationStats);

            Assert.IsType <EventId>(res);
            Assert.Equal(10300, res.Id);
            Assert.Equal("GetNationStats", res.Name);
            var logString = LogMessageBuilder.Build(res, "Test");

            Assert.Equal("[10300] Test", logString);
        }
Ejemplo n.º 11
0
        public void TestLogMessageBuilder()
        {
            var res = CyborgianStates.Helpers.GetEventIdByType(Enums.LoggingEvent.GetNationStats);

            res.Should().BeOfType <EventId>();
            res.Id.Should().Be(10300);
            res.Name.Should().Be("GetNationStats");
            var logString = LogMessageBuilder.Build(res, "Test");

            logString.Should().Be("[10300] Test");
        }
Ejemplo n.º 12
0
        private async Task EnsurePoolFilledAsync()
        {
            var           id = LogEventIdProvider.GetEventIdByType(LoggingEvent.EnsurePoolFilled);
            List <REGION> regionsToRecruitFrom = await GetRegionToRecruitFromAsync(id);

            while (IsRecruiting)
            {
                bool fillingUp    = false;
                int  counter      = 0;
                int  pendingCount = NationManager.GetNationCountByStatusName("pending");
                while (pendingCount < _config.MinimumRecruitmentPoolSize)
                {
                    if (!fillingUp)
                    {
                        fillingUp = true;
                        _logger.LogInformation(id, LogMessageBuilder.Build(id, $"Filling up pending pool now from {pendingCount} to {_config.MinimumRecruitmentPoolSize}"));
                    }
                    PoolStatus = "Filling up with random nations";
                    var    regionId = _rnd.Next(regionsToRecruitFrom.Count);
                    var    region   = regionsToRecruitFrom.ElementAt(regionId);
                    string nationName;
                    do
                    {
                        var nationId = _rnd.Next(region.NATIONNAMES.Count);
                        nationName = region.NATIONNAMES.ElementAt(nationId);
                    }while (await NationManager.IsNationPendingSkippedSendOrFailedAsync(nationName) || await IsNationRecruitableAsync(new Nation()
                    {
                        Name = nationName, StatusTime = DateTime.UtcNow
                    }, id));
                    var nation = await NationManager.GetNationAsync(nationName);

                    if (nation != null)
                    {
                        await NationManager.SetNationStatusToAsync(nation, "pending");
                    }
                    else
                    {
                        await NationManager.AddUnknownNationsAsPendingAsync(new List <string>() { nationName }, true);
                    }
                    counter++;
                    pendingCount = NationManager.GetNationCountByStatusName("pending");
                    _logger.LogDebug(id, LogMessageBuilder.Build(id, $"Added nation '{nationName}' to pending. Now at {pendingCount} from minimum {_config.MinimumRecruitmentPoolSize}."));
                }
                if (fillingUp)
                {
                    _logger.LogInformation(id, LogMessageBuilder.Build(id, $"Filled up pending pool to minimum. (Added {counter} nations to pending.)"));
                    PoolStatus = "Waiting for new nations";
                }

                await Task.Delay(1800000); //30 min
            }
        }
Ejemplo n.º 13
0
 public void StartRecruitment()
 {
     if (!IsRecruiting)
     {
         IsRecruiting = true;
         Task.Run(async() => await GetNewNationsAsync());
         Task.Run(async() => await EnsurePoolFilledAsync());
         Task.Run(async() => await RecruitAsync());
         RecruitmentStatus = "Started";
         Task.Run(async() => await UpdateRecruitmentStatsAsync());
         _logger.LogInformation(_defaulEventId, LogMessageBuilder.Build(_defaulEventId, "Recruitment process started."));
     }
 }
Ejemplo n.º 14
0
 private async Task <bool> IsNationRecruitableAsync(string nationName, EventId id)
 {
     if (!string.IsNullOrWhiteSpace(nationName))
     {
         if (!await DoesNationFitCriteriaAsync(nationName) || !await WouldReceiveTelegram(nationName))
         {
             _logger.LogDebug(id, LogMessageBuilder.Build(id, $"{nationName} does not fit criteria and is therefore skipped"));
             return(false);
         }
         return(true);
     }
     return(false);
 }
        private void FormatterIsCalled()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions();

            var isCalled = false;

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null, (state, ex) => { isCalled = true; return("called"); });

            Assert.True(isCalled);
        }
        public async Task DoGetRNStatusAsync()
        {
            var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.RNSCommand);

            try
            {
                await ReplyAsync(_recruitmentService.GetRNStatus());
            }
            catch (Exception ex)
            {
                _logger.LogCritical(id, ex, LogMessageBuilder.Build(id, "A critical error occured"));
                await ReplyAsync($"Something went wrong :( ");
            }
        }
Ejemplo n.º 17
0
        public async Task ProcessMessageAsync(object message)
        {
            var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.UserMessage);

            try
            {
                if (message is SocketUserMessage socketMsg)
                {
                    var context = new SocketCommandContext(DiscordClient, socketMsg);
                    if (Reactive)
                    {
                        _logger.LogDebug(id, LogMessageBuilder.Build(id, $"{socketMsg.Author.Username} in {socketMsg.Channel.Name}: {socketMsg.Content}"));
                        if (await IsRelevantAsync(message, context.User))
                        {
                            //Disables Reactiveness of the bot to commands. Ignores every command until waked up using the /wakeup command.
                            if (await _permManager.IsBotAdminAsync(context.User) && socketMsg.Content == $"{_config.SeperatorChar}sleep")
                            {
                                await context.Client.SetStatusAsync(UserStatus.DoNotDisturb);

                                await context.Channel.SendMessageAsync($"Ok! Going to sleep now. Wake me up later with {_config.SeperatorChar}wakeup.");

                                Reactive = false;
                            }
                            else
                            {
                                await commandService.ExecuteAsync(context, 1, Program.ServiceProvider);
                            }
                        }
                    }
                    else
                    {
                        if (await _permManager.IsBotAdminAsync(context.User) && socketMsg.Content == $"{_config.SeperatorChar}wakeup")
                        {
                            Reactive = true;
                            await context.Client.SetStatusAsync(UserStatus.Online);

                            await context.Channel.SendMessageAsync("Hey! I'm back.");
                        }
                        else if (await IsRelevantAsync(message, context.User) && context.Client.Status == UserStatus.DoNotDisturb && !await _permManager.IsBotAdminAsync(context.User))
                        {
                            await context.Channel.SendMessageAsync(AppSettings.SLEEPTEXT);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical(id, ex, LogMessageBuilder.Build(id, "A critical error occured."));
            }
        }
Ejemplo n.º 18
0
        public async Task <List <Nation> > GetRecruitableNationsAsync(int number, bool isAPI)
        {
            List <Nation> returnNations = new List <Nation>();
            var           id            = LogEventIdProvider.GetEventIdByType(LoggingEvent.GetRecruitableNations);

            try
            {
                _logger.LogDebug(id, LogMessageBuilder.Build(id, $"{number} recruitable nations requested"));
                List <Nation> pendingNations = new List <Nation>();
                if (pendingNations.Count == 0)
                {
                    pendingNations = NationManager.GetNationsByStatusName("pending");
                }
                while (returnNations.Count < number)
                {
                    var picked = pendingNations.Take(1);
                    var nation = picked.Count() > 0 ? picked.ToArray()[0] : null;
                    if (nation != null)
                    {
                        if (await IsNationRecruitableAsync(nation, id))
                        {
                            returnNations.Add(nation);
                            if (IsReceivingRecruitableNations && !isAPI)
                            {
                                currentRNStatus.CurrentCount++;
                            }
                        }
                        pendingNations.Remove(nation);
                        returnNations = returnNations.Distinct().ToList();
                    }
                    else
                    {
                        if (pendingNations.Count == 0)
                        {
                            _logger.LogCritical(id, "No more nations in pending pool !");
                            return(returnNations);
                        }
                        else
                        {
                            _logger.LogCritical(id, "Picked nation was null !");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(id, ex, LogMessageBuilder.Build(id, "An error occured."));
            }
            return(returnNations);
        }
        private void IPAddressIsSet()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            Assert.Equal(builder.LocalIPAddress, message.Header.IPAddress);
        }
        private void MessageVersionIsSet()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            Assert.Equal(options.MessageVersion, message.Body.VersionNumber);
        }
        private void CorrelationIdIsSetWithoutRegisteredCorrelationContext()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            Assert.NotNull(message.Header.Correlation.CorrelationId);
        }
Ejemplo n.º 22
0
        public async Task <bool> WouldReceiveTelegram(string nationName)
        {
            XmlDocument result = await _apiService.GetWouldReceiveTelegramAsync(nationName);

            if (result != null)
            {
                XmlNodeList canRecruitNodeList = result.GetElementsByTagName("TGCANRECRUIT");
                return(canRecruitNodeList[0].InnerText == "1");
            }
            else
            {
                var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.WouldReceiveTelegram);
                _logger.LogWarning(id, LogMessageBuilder.Build(id, $"Result of GetWouldReceiveTelegramAsync '{nationName}' were null"));
                return(false);
            }
        }
        private void LevelIsConverted()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new Mock <ILogLevelConverter>();
            var options         = new LogstashOptions();

            var level = LogLevel.Information;

            converter.Setup((c) => c.ToLogStashLevel(level)).Returns(LogstashLevel.Information).Verifiable();

            var builder = new LogMessageBuilder(serviceProvider, converter.Object, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            converter.Verify();
        }
Ejemplo n.º 24
0
        public static async Task RemoveUserFromDbAsync(string userId)
        {
            using (var dbContext = new BotDbContext(_config))
            {
                var user = await dbContext.Users.FirstOrDefaultAsync(u => u.DiscordUserId == userId);

                if (user != null)
                {
                    var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.UserDbAction);
                    dbContext.Remove(user);
                    await dbContext.SaveChangesAsync();

                    logger.LogInformation(id, LogMessageBuilder.Build(id, $"Removed User {userId} from database"));
                }
            }
        }
        protected async Task <HttpResponseMessage> ExecuteGetRequestAsync(string url, EventId?eventId)
        {
            bool releaseId = false;
            var  logId     = eventId != null ? (EventId)eventId : LogEventIdProvider.GetRandomLogEventId();

            lastAPIRequest = DateTime.UtcNow;
            if (eventId == null)
            {
                releaseId = true;
            }
            try
            {
                using (var client = new HttpClient())
                {
                    _logger.LogDebug(logId, LogMessageBuilder.Build(logId, $"Executing Request to {url}"));
                    client.DefaultRequestHeaders.Add("User-Agent", $"NationStatesApiBot/{AppSettings.VERSION}");
                    client.DefaultRequestHeaders.Add("User-Agent", $"(contact { _config.Contact};)");
                    var response = await client.GetAsync(url);

                    if (!response.IsSuccessStatusCode)
                    {
                        _logger.LogError(logId, LogMessageBuilder.Build(logId, $"Request finished with response: {(int) response.StatusCode}: {response.ReasonPhrase}"));
                    }
                    else
                    {
                        _logger.LogDebug(logId, LogMessageBuilder.Build(logId, $"Request finished with response: {(int) response.StatusCode}: {response.ReasonPhrase}"));
                    }
                    if ((int)response.StatusCode == 429)
                    {
                        _logger.LogDebug(logId, LogMessageBuilder.Build(logId, $"Retry in {response.Headers.RetryAfter.Delta} seconds."));
                    }
                    if (((int)response.StatusCode).ToString().StartsWith("5"))
                    {
                        throw new ApplicationException($"Server Side Error while Executing Request => {(int) response.StatusCode}: {response.ReasonPhrase}");
                    }
                    return(response);
                }
            }
            finally
            {
                if (releaseId)
                {
                    LogEventIdProvider.ReleaseEventId(logId);
                }
            }
        }
        private void TimestampIsSet()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            var now = DateTime.Now;

            Assert.InRange(message.Header.TimeStamp, now.AddMinutes(-1), now.AddMinutes(1));
        }
Ejemplo n.º 27
0
        private async Task AddNationToPendingAsync(EventId id, List <string> nationNames, bool isSourceDumps)
        {
            List <string> nationsToAdd = new List <string>();

            foreach (var res in nationNames)
            {
                if (await DoesNationFitCriteriaAsync(res))
                {
                    nationsToAdd.Add(res);
                }
            }
            var counter = await NationManager.AddUnknownNationsAsPendingAsync(nationsToAdd, isSourceDumps);

            if (counter > 0)
            {
                _logger.LogInformation(id, LogMessageBuilder.Build(id, $"{counter} nations added to pending"));
            }
        }
        private async Task <HttpResponseMessage> GetNationStatsAsync(string nationName, EventId eventId)
        {
            _logger.LogDebug(eventId, LogMessageBuilder.Build(eventId, $"Waiting for NationStats-Request: {nationName}"));
            await WaitForAction(RequestType.GetBasicNationStats).ConfigureAwait(false);

            Uri url     = BuildApiRequestUrl($"nation={Helpers.ToID(nationName)}&q=flag+wa+gavote+scvote+fullname+freedom+demonym2plural+category+population+region+founded+influence+lastactivity+census;mode=score;scale=0+1+2+65+66+80");
            var message = new HttpRequestMessage(HttpMethod.Get, url);

            LastAPIRequest = DateTime.UtcNow;
            try
            {
                return(await _dataService.ExecuteRequest(message, eventId).ConfigureAwait(false));
            }
            finally
            {
                message.Dispose();
            }
        }
        private void CorrelationIdIsSetFromRegisteredCorrelationContext()
        {
            var correlationContext = new Mock <ICorrelationContext>();
            var serviceProvider    = new Mock <IServiceProvider>();
            var converter          = new LogLevelConverter();
            var options            = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            correlationContext.SetupGet(p => p.CorrelationId).Returns("correlation-id");
            serviceProvider.Setup(sp => sp.GetService(typeof(ICorrelationContext))).Returns(correlationContext.Object);

            var builder = new LogMessageBuilder(serviceProvider.Object, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            Assert.Equal("correlation-id", message.Header.Correlation.CorrelationId);
        }
Ejemplo n.º 30
0
        public async Task DoGetRNStatus()
        {
            var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.RNSCommand);

            try
            {
                if (await _permManager.IsAllowedAsync(PermissionType.ManageRecruitment, Context.User))
                {
                    await ReplyAsync(_recruitmentService.GetRNStatus());
                }
                else
                {
                    await ReplyAsync(AppSettings.PERMISSION_DENIED_RESPONSE);
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical(id, ex, LogMessageBuilder.Build(id, "An critical error occured"));
                await ReplyAsync($"Something went wrong :( ");
            }
        }