コード例 #1
0
        private static ScannedResult ScanLines(string[] lines, PagedLogItems currentPage)
        {
            var result = new ScannedResult();

            if (lines == null || lines.Length <= 0)
            {
                return(result);
            }

            if (currentPage == null || currentPage.LogItems == null)
            {
                return(result);
            }

            if (currentPage.LogItems.Count >= PAGED_LINES_COUNT)
            {
                currentPage.LogItems.Clear();
            }

            foreach (var line in lines)
            {
                if (currentPage.LogItems.Count >= PAGED_LINES_COUNT)
                {
                    break;
                }

                var logItem = line.ToLogItem(currentPage.LogType);
                if (logItem == null)
                {
                    result.ScannedByteLength += System.Text.Encoding.UTF8.GetBytes(line + "\n").LongLength;
                    continue;
                }
                else
                {
                    result.ScannedItemsCount++;
                    currentPage.LogItems.Add(logItem);
                    result.ScannedByteLength += System.Text.Encoding.UTF8.GetBytes(line + "\n").LongLength;
                }
            }
            return(result);
        }
コード例 #2
0
        private static PagedLogItems LoadLogItemByPage(PagedLogItems currentPage)
        {
            var stopWatch = new Stopwatch();

            Console.WriteLine("Start load log items from file \"test.log\"....");
            stopWatch.Start();

            var buffer = new byte[BUFFER_SIZE_OF_96_KB];

            using (var fs = new FileStream("test.log", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                currentPage.TotalSize = fs.Length;
                var errorCount = 0;
                SetPositionOfFileStream(currentPage, fs);
                while (fs.Read(buffer, 0, BUFFER_SIZE_OF_96_KB) > 0)
                {
                    var content = System.Text.Encoding.UTF8.GetString(buffer);
                    content = content.TrimEnd('\0');
                    if (string.IsNullOrEmpty(content))
                    {
                        break;
                    }

                    var lines = content.Split('\n');
                    if (lines == null || lines.Length <= 0)
                    {
                        break;
                    }

                    ScannedResult scannedResult = null;
                    try
                    {
                        scannedResult = ScanLines(lines, currentPage);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ScanLines Failed! error message:{0}", ex.Message);

                        errorCount++;
                        currentPage.LogItems = currentPage.LogItems.Take(currentPage.LogItems.Count - scannedResult.ScannedItemsCount).ToList();
                        buffer = new byte[BUFFER_SIZE_OF_96_KB];
                        var newStartPosition = (fs.Position - BUFFER_SIZE_OF_96_KB - errorCount) > 0 ? (fs.Position - BUFFER_SIZE_OF_96_KB - errorCount) : 0;
                        fs.Position = newStartPosition;
                        continue;
                    }

                    errorCount = 0;

                    var currentStartPosition = (fs.Position - BUFFER_SIZE_OF_96_KB);
                    var currentEndPosition   = (currentStartPosition + scannedResult.ScannedByteLength) >= fs.Length ? fs.Length : currentStartPosition + scannedResult.ScannedByteLength;
                    if (currentPage.CurrentStartPosition == null || currentPage.CurrentStartPosition.Count <= 0)
                    {
                        currentPage.PreviousStartPosition = 0;
                    }
                    else
                    {
                        currentPage.PreviousStartPosition = currentPage.CurrentStartPosition.Last().Position;
                    }

                    //Load give count of log items in the first loading
                    if (currentPage.LogItems.Count >= PAGED_LINES_COUNT)
                    {
                        currentPage.CurrentStartPosition.Add(new PositionWithStatus()
                        {
                            Position = currentStartPosition, LoadCompleted = true
                        });
                        currentPage.CurrentEndPosition.Add(new PositionWithStatus()
                        {
                            Position = currentEndPosition, LoadCompleted = true
                        });
                        break;
                    }
                    else
                    {
                        currentPage.CurrentStartPosition.Add(new PositionWithStatus()
                        {
                            Position = currentStartPosition, LoadCompleted = false
                        });
                        currentPage.CurrentEndPosition.Add(new PositionWithStatus()
                        {
                            Position = currentEndPosition, LoadCompleted = false
                        });

                        buffer = new byte[BUFFER_SIZE_OF_96_KB];
                        SetPositionOfFileStream(currentPage, fs);
                    }
                }
            }

            stopWatch.Stop();
            Console.WriteLine("Loading log items from file \"test.log\" ended.");
            Console.WriteLine("Time consumed:{0} milliseconds", stopWatch.ElapsedMilliseconds);
            return(currentPage);
        }