private IEnumerable <Line> ReadLinesByIndex(ScrollRequest scroll)
        {
            var page = GetPage(scroll);

            var relativeIndex = CalculateRelativeIndex(page.Start);

            if (relativeIndex == null)
            {
                yield break;
            }

            var offset = relativeIndex.LinesOffset;

            using (var stream = File.Open(Info.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
            {
                using (var reader = new StreamReaderExtended(stream, Encoding, false))
                {
                    //go to starting point
                    stream.Seek(relativeIndex.Start, SeekOrigin.Begin);
                    if (offset > 0)
                    {
                        //skip number of lines offset
                        for (int i = 0; i < offset; i++)
                        {
                            reader.ReadLine();
                        }
                    }

                    //if estimate move to the next start of line
                    if (relativeIndex.IsEstimate && relativeIndex.Start != 0)
                    {
                        reader.ReadLine();
                    }

                    foreach (var i in Enumerable.Range(page.Start, page.Size))
                    {
                        var startPosition = reader.AbsolutePosition();
                        var line          = reader.ReadLine();
                        var endPosition   = reader.AbsolutePosition();
                        var info          = new LineInfo(i + 1, i, startPosition, endPosition);

                        var ontail = startPosition >= TailInfo.TailStartsAt && DateTime.UtcNow.Subtract(TailInfo.LastTail).TotalSeconds < 1
                            ? DateTime.UtcNow
                            : (DateTime?)null;

                        if (!line.StartsWith("{"))
                        {
                            yield return(new Line(info, line, ontail));
                        }
                        else
                        {
                            var    data            = JsonSerializer.Deserialize <Dictionary <string, object> >(line);
                            var    timestamp       = DateTime.Parse(data["@t"].ToString());
                            string message         = data["@mt"].ToString();
                            var    timestampPrefix = timestamp.ToString("[dd/MM/yyyy hh:mm:ss.fff tt] ");

                            string formattedMessage = SubstituteRegex.Replace(message, match =>
                            {
                                var name = match.Groups["exp"].Value;
                                return(data[name].ToString());
                            });

                            if (data.TryGetValue("@x", out var exception) && !string.IsNullOrEmpty(exception.ToString()))
                            {
                                yield return(new Line(info, timestampPrefix + formattedMessage + " (" + exception.ToString() + ")", ontail));
                            }
                            else
                            {
                                yield return(new Line(info, timestampPrefix + formattedMessage, ontail));
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        private IEnumerable <Line> ReadLinesByIndex(ScrollRequest scroll)
        {
            var current          = this;
            var lastValueWrapper = new LastValueWrapper();
            var iterationCounter = 0;
            var page             = GetPage(scroll, current);
            var relativeIndex    = CalculateRelativeIndex(page.Start, ref current, lastValueWrapper);

            while (relativeIndex != null && current != null)
            {
                if (current.Indicies.Length > 0 && current.Indicies.Any(t => t.Indicies.Count == 0))
                {
                    iterationCounter++;
                    current = current.Previous as IndexCollection;
                    continue;
                }
                if (lastValueWrapper.LastPageIndex == page.Start + page.Size)
                {
                    yield break;
                }
                var offset = relativeIndex.LinesOffset;
                using (
                    var stream = File.Open(current.Info.FullName, FileMode.Open, FileAccess.Read,
                                           FileShare.Delete | FileShare.ReadWrite))
                {
                    using (var reader = new StreamReaderExtended(stream, current.Encoding, false))
                    {
                        //go to starting point
                        stream.Seek((iterationCounter > 0) ? 0 : relativeIndex.Start, SeekOrigin.Begin);
                        if (iterationCounter == 0 && offset > 0)
                        {
                            //skip number of lines offset
                            for (var i = 0; i < offset; i++)
                            {
                                reader.ReadLine();
                            }
                        }

                        //if estimate move to the next start of line
                        if (iterationCounter == 0 && relativeIndex.IsEstimate && relativeIndex.Start != 0)
                        {
                            reader.ReadLine();
                        }

                        foreach (
                            var i in
                            Enumerable.Range((iterationCounter > 0) ? lastValueWrapper.LastPageIndex : page.Start,
                                             page.Size))
                        {
                            if (i == page.Start + page.Size)
                            {
                                yield break;
                            }
                            var startPosition = reader.AbsolutePosition() + lastValueWrapper.LastEndPosition;
                            var line          = reader.ReadLine();
                            var endPosition   = reader.AbsolutePosition() + lastValueWrapper.LastEndPosition;

                            var info = new LineInfo(i + 1, i, startPosition, endPosition);

                            var ontail = startPosition >= current.TailInfo.TailStartsAt &&
                                         DateTime.Now.Subtract(current.TailInfo.LastTail).TotalSeconds < 1
                                ? DateTime.Now
                                : (DateTime?)null;

                            yield return(new Line(info, line, ontail));

                            lastValueWrapper.LastPageIndex = i + 1;

                            if (reader.EndOfStream)
                            {
                                lastValueWrapper.LastEndPosition += endPosition + 1;
                                break;
                            }
                        }
                    }
                }
                iterationCounter++;
                current = current.Previous as IndexCollection;
            }
        }