Example #1
0
    public int GetNumberOfErrors(LogTimePeriod logTimePeriod)
    {
        var errorCounter = new ErrorCounterFilter();

        GetLogs(logTimePeriod, errorCounter, 0, int.MaxValue);
        return(errorCounter.Count);
    }
Example #2
0
    public PagedResult <LogMessage> GetLogs(
        LogTimePeriod logTimePeriod,
        int pageNumber           = 1,
        int pageSize             = 100,
        Direction orderDirection = Direction.Descending,
        string?filterExpression  = null,
        string[]?logLevels       = null)
    {
        var expression = new ExpressionFilter(filterExpression);
        IReadOnlyList <LogEvent> filteredLogs = GetLogs(logTimePeriod, expression, 0, int.MaxValue);

        // This is user used the checkbox UI to toggle which log levels they wish to see
        // If an empty array or null - its implied all levels to be viewed
        if (logLevels?.Length > 0)
        {
            var logsAfterLevelFilters = new List <LogEvent>();
            var validLogType          = true;
            foreach (var level in logLevels)
            {
                // Check if level string is part of the LogEventLevel enum
                if (Enum.IsDefined(typeof(LogEventLevel), level))
                {
                    validLogType = true;
                    logsAfterLevelFilters.AddRange(filteredLogs.Where(x =>
                                                                      string.Equals(x.Level.ToString(), level, StringComparison.InvariantCultureIgnoreCase)));
                }
                else
                {
                    validLogType = false;
                }
            }

            if (validLogType)
            {
                filteredLogs = logsAfterLevelFilters;
            }
        }

        long totalRecords = filteredLogs.Count;

        // Order By, Skip, Take & Select
        IEnumerable <LogMessage> logMessages = filteredLogs
                                               .OrderBy(l => l.Timestamp, orderDirection)
                                               .Skip(pageSize * (pageNumber - 1))
                                               .Take(pageSize)
                                               .Select(x => new LogMessage
        {
            Timestamp           = x.Timestamp,
            Level               = x.Level,
            MessageTemplateText = x.MessageTemplate.Text,
            Exception           = x.Exception?.ToString(),
            Properties          = x.Properties,
            RenderedMessage     = x.RenderMessage(),
        });

        return(new PagedResult <LogMessage>(totalRecords, pageNumber, pageSize)
        {
            Items = logMessages
        });
    }
Example #3
0
    public LogLevelCounts GetLogLevelCounts(LogTimePeriod logTimePeriod)
    {
        var counter = new CountingFilter();

        GetLogs(logTimePeriod, counter, 0, int.MaxValue);
        return(counter.Counts);
    }
Example #4
0
    public override bool CheckCanOpenLogs(LogTimePeriod logTimePeriod)
    {
        // Log Directory
        var logDirectory = _logsPath;

        // Number of entries
        long fileSizeCount = 0;

        // foreach full day in the range - see if we can find one or more filenames that end with
        // yyyyMMdd.json - Ends with due to MachineName in filenames - could be 1 or more due to load balancing
        for (DateTime day = logTimePeriod.StartTime.Date; day.Date <= logTimePeriod.EndTime.Date; day = day.AddDays(1))
        {
            // Filename ending to search for (As could be multiple)
            var filesToFind = GetSearchPattern(day);

            var filesForCurrentDay = Directory.GetFiles(logDirectory, filesToFind);

            fileSizeCount += filesForCurrentDay.Sum(x => new FileInfo(x).Length);
        }

        // The GetLogSize call on JsonLogViewer returns the total file size in bytes
        // Check if the log size is not greater than 100Mb (FileSizeCap)
        var logSizeAsMegabytes = fileSizeCount / 1024 / 1024;

        return(logSizeAsMegabytes <= FileSizeCap);
    }
Example #5
0
    protected override IReadOnlyList <LogEvent> GetLogs(LogTimePeriod logTimePeriod, ILogFilter filter, int skip,
                                                        int take)
    {
        var logs = new List <LogEvent>();

        var count = 0;

        // foreach full day in the range - see if we can find one or more filenames that end with
        // yyyyMMdd.json - Ends with due to MachineName in filenames - could be 1 or more due to load balancing
        for (DateTime day = logTimePeriod.StartTime.Date; day.Date <= logTimePeriod.EndTime.Date; day = day.AddDays(1))
        {
            // Filename ending to search for (As could be multiple)
            var filesToFind = GetSearchPattern(day);

            var filesForCurrentDay = Directory.GetFiles(_logsPath, filesToFind);

            // Foreach file we find - open it
            foreach (var filePath in filesForCurrentDay)
            {
                // Open log file & add contents to the log collection
                // Which we then use LINQ to page over
                using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (var stream = new StreamReader(fs))
                    {
                        var reader = new LogEventReader(stream);
                        while (TryRead(reader, out LogEvent? evt))
                        {
                            // We may get a null if log line is malformed
                            if (evt == null)
                            {
                                continue;
                            }

                            if (count > skip + take)
                            {
                                break;
                            }

                            if (count < skip)
                            {
                                count++;
                                continue;
                            }

                            if (filter.TakeLogEvent(evt))
                            {
                                logs.Add(evt);
                            }

                            count++;
                        }
                    }
                }
            }
        }

        return(logs);
    }
Example #6
0
        private bool CanViewLogs(LogTimePeriod logTimePeriod)
        {
            //Can the interface deal with Large Files
            if (_logViewer.CanHandleLargeLogs)
            {
                return(true);
            }

            //Interface CheckCanOpenLogs
            return(_logViewer.CheckCanOpenLogs(logTimePeriod));
        }
Example #7
0
    public ActionResult <IEnumerable <LogTemplate> > GetMessageTemplates([FromQuery] DateTime?startDate = null,
                                                                         [FromQuery] DateTime?endDate   = null)
    {
        LogTimePeriod logTimePeriod = GetTimePeriod(startDate, endDate);

        //We will need to stop the request if trying to do this on a 1GB file
        if (CanViewLogs(logTimePeriod) == false)
        {
            return(ValidationProblem("Unable to view logs, due to size"));
        }

        return(new ActionResult <IEnumerable <LogTemplate> >(_logViewer.GetMessageTemplates(logTimePeriod)));
    }
Example #8
0
    public ActionResult <int> GetNumberOfErrors([FromQuery] DateTime?startDate = null,
                                                [FromQuery] DateTime?endDate   = null)
    {
        LogTimePeriod logTimePeriod = GetTimePeriod(startDate, endDate);

        //We will need to stop the request if trying to do this on a 1GB file
        if (CanViewLogs(logTimePeriod) == false)
        {
            return(ValidationProblem("Unable to view logs, due to size"));
        }

        return(_logViewer.GetNumberOfErrors(logTimePeriod));
    }
Example #9
0
    public IEnumerable <LogTemplate> GetMessageTemplates(LogTimePeriod logTimePeriod)
    {
        var messageTemplates = new MessageTemplateFilter();

        GetLogs(logTimePeriod, messageTemplates, 0, int.MaxValue);

        IOrderedEnumerable <LogTemplate> templates = messageTemplates.Counts
                                                     .Select(x => new LogTemplate {
            MessageTemplate = x.Key, Count = x.Value
        })
                                                     .OrderByDescending(x => x.Count);

        return(templates);
    }
Example #10
0
    public ActionResult <PagedResult <LogMessage> > GetLogs(string orderDirection          = "Descending", int pageNumber = 1,
                                                            string?filterExpression        = null, [FromQuery(Name = "logLevels[]")] string[]?logLevels = null,
                                                            [FromQuery] DateTime?startDate = null, [FromQuery] DateTime?endDate = null)
    {
        LogTimePeriod logTimePeriod = GetTimePeriod(startDate, endDate);

        //We will need to stop the request if trying to do this on a 1GB file
        if (CanViewLogs(logTimePeriod) == false)
        {
            return(ValidationProblem("Unable to view logs, due to size"));
        }

        Direction direction = orderDirection == "Descending" ? Direction.Descending : Direction.Ascending;

        return(_logViewer.GetLogs(logTimePeriod, filterExpression: filterExpression, pageNumber: pageNumber,
                                  orderDirection: direction, logLevels: logLevels));
    }
Example #11
0
        public void ProcessData(Stream stream, LogTimePeriod timePeriod, IEnumerable <LogMessage> messages)
        {
            var logMessages = messages.ToList();

            if (!logMessages.Any())
            {
                return;
            }

            using (var excel = new ExcelPackage())
            {
                var sheet = excel.Workbook.Worksheets.Add($"{timePeriod.StartTime:yyyy-MM-dd} to {timePeriod.EndTime:yyyy-MM-dd}");

                var row = 0;

                SetHeaderCell(sheet, row, 0, "Timestamp");
                SetHeaderCell(sheet, row, 1, "Level");
                SetHeaderCell(sheet, row, 2, "Message");
                SetHeaderCell(sheet, row, 3, "Exception");
                SetHeaderCell(sheet, row, 4, "Properties");

                row++;

                var hasExceptions = false;

                foreach (var logMessage in logMessages)
                {
                    //SetDateTimeCell(sheet, row, 1, logMessage.Timestamp);
                    SetCell(sheet, row, 0, logMessage.Timestamp.ToString());
                    SetCell(sheet, row, 1, logMessage.Level);
                    SetGeneralCellWithWrappedText(sheet, row, 2, logMessage.RenderedMessage);

                    if (!string.IsNullOrEmpty(logMessage.Exception))
                    {
                        hasExceptions = true;
                    }
                    SetGeneralCellWithWrappedText(sheet, row, 3, logMessage.Exception);

                    var properties = new StringBuilder();

                    foreach (var property in logMessage.Properties)
                    {
                        properties.AppendLine($"{property.Key}: {property.Value.ToString()}");
                    }

                    SetGeneralCellWithWrappedText(sheet, row, 4, properties.ToString());

                    row++;
                }

                var allCells = sheet.Cells[1, 1, logMessages.Count(), 5];
                allCells.AutoFitColumns();
                allCells.Style.VerticalAlignment = ExcelVerticalAlignment.Top;

                sheet.Column(3).Width = 100;
                sheet.Column(4).Width = 100;
                sheet.Column(5).Width = 100;

                if (!hasExceptions)
                {
                    sheet.Column(4).Hidden = true;
                }

                excel.SaveAs(stream);
            }
        }
Example #12
0
 public abstract bool CheckCanOpenLogs(LogTimePeriod logTimePeriod);
Example #13
0
 /// <summary>
 ///     Get all logs from your chosen data source back as Serilog LogEvents
 /// </summary>
 protected abstract IReadOnlyList <LogEvent> GetLogs(LogTimePeriod logTimePeriod, ILogFilter filter, int skip, int take);
 private bool CanViewLogs(LogTimePeriod logTimePeriod)
 {
     return(_logViewer.CanHandleLargeLogs || _logViewer.CheckCanOpenLogs(logTimePeriod));
 }
Example #15
0
    public bool GetCanViewLogs([FromQuery] DateTime?startDate = null, [FromQuery] DateTime?endDate = null)
    {
        LogTimePeriod logTimePeriod = GetTimePeriod(startDate, endDate);

        return(CanViewLogs(logTimePeriod));
    }