public int GetNumberOfErrors(LogTimePeriod logTimePeriod)
        {
            var errorCounter = new ErrorCounterFilter();

            GetLogs(logTimePeriod, errorCounter, 0, int.MaxValue);
            return(errorCounter.Count);
        }
        public LogLevelCounts GetLogLevelCounts(LogTimePeriod logTimePeriod)
        {
            var counter = new CountingFilter();

            GetLogs(logTimePeriod, counter, 0, int.MaxValue);
            return(counter.Counts);
        }
Exemple #3
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 (var 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);
        }
Exemple #4
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 (var 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 var 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);
        }
        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);
            var 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
            var 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
            });
        }
        public IEnumerable <LogTemplate> GetMessageTemplates(LogTimePeriod logTimePeriod)
        {
            var messageTemplates = new MessageTemplateFilter();

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

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

            return(templates);
        }
 public abstract bool CheckCanOpenLogs(LogTimePeriod logTimePeriod);
 /// <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);