示例#1
0
        public async Task <IActionResult> Logs(LogMessagesFilterViewModel model)
        {
            if (model.Id.HasValue)
            {
                return(RedirectToActionPermanent("Log", new { id = model.Id.Value }));
            }

            try
            {
                ViewBag.Messages = await _loggingService.GetLogMessagesAsync(model.ToLogMessagesFilterModel());
            }
            catch (System.Exception e)
            {
                _logger.LogWarning(LoggingEvents.HomeController.AsInt(), e, "Bad filter for logs");
                return(BadRequest("Bad filter parameters"));
            }

            ViewBag.FilterData = await _loggingService.GetAvailableFilterDataAsync();

            if (ViewBag.Messages.Count == 0)
            {
                TempData["MessageSeverity"] = "warning";
                TempData["MessageContent"]  = $"No log messages found in the system.";
            }

            return(View(model));
        }
示例#2
0
        public async Task GetsAvailableFilterData()
        {
            // Arrange
            var twoDaysAgo = DateTime.UtcNow.AddDays(-2);
            var hourAgo    = DateTime.UtcNow.AddHours(-1);
            var now        = DateTime.UtcNow;

            await _context.LogEntries.AddRangeAsync(
                new List <LogEntry> {
                new LogEntry {
                    Severity = new LogEntrySeverity {
                        Id = LogEntrySeverities.Debug.AsInt()
                    },
                    Category  = LoggingEvents.Clean.AsInt(),
                    Source    = "the-source-1",
                    Timestamp = twoDaysAgo
                },
                new LogEntry {
                    Severity = new LogEntrySeverity {
                        Id = LogEntrySeverities.Info.AsInt()
                    },
                    Category  = LoggingEvents.Clean.AsInt(),
                    Source    = "the-source-2",
                    Timestamp = twoDaysAgo
                },
                new LogEntry {
                    Severity = new LogEntrySeverity {
                        Id = LogEntrySeverities.Debug.AsInt()
                    },
                    Category  = LoggingEvents.Clean.AsInt(),
                    Source    = "the-source-1",
                    Timestamp = hourAgo
                },
                new LogEntry {
                    Severity = new LogEntrySeverity {
                        Id = LogEntrySeverities.Error.AsInt()
                    },
                    Category  = LoggingEvents.ActionFilters.AsInt(),
                    Source    = "the-source-1",
                    Timestamp = now
                }
            }
                );

            await _context.SaveChangesAsync();

            var expected = new LogMessagesFilterModel
            {
                Severities = new List <LogEntrySeverities> {
                    LogEntrySeverities.Debug,
                    LogEntrySeverities.Info,
                    LogEntrySeverities.Error
                },
                Sources = new List <string> {
                    "the-source-1",
                    "the-source-2"
                },
                Categories = new List <int> {
                    LoggingEvents.Clean.AsInt(),
                LoggingEvents.ActionFilters.AsInt()
                },
                Start = twoDaysAgo,
                End   = now
            };

            // Act
            var actual = await _loggingService.GetAvailableFilterDataAsync();

            // Assert
            Assert.Equal(expected.Categories, actual.Categories);
            Assert.Equal(expected.Severities, actual.Severities);
            Assert.Equal(expected.Sources, actual.Sources);
            Assert.Equal(expected.Start, actual.Start);
            Assert.Equal(expected.End, actual.End);
        }