Пример #1
0
        protected override void ReadFile()
        {
            _progress.Report($"{DateTime.Now} | Checking file");
            if (!File.Exists(path))
            {
                return;
            }

            var createdDate = File.GetCreationTime(path);

            var logFile = logFileRepository.GetFileByName(path);

            if (logFile != null)
            {
                if (createdDate.MongoEquals(logFile.Created))
                {
                    logFile.ReadBytes = 0;
                }

                try
                {
                    ReadLines(logFile);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Read file exception");
                }

                logFileRepository.UpdateFile(logFile);
            }
            else
            {
                _progress.Report("new file");
                logFile = new LogFile
                {
                    Path      = path,
                    Created   = File.GetCreationTime(path),
                    ReadBytes = 0
                };
                try
                {
                    ReadLines(logFile);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Read file exception");
                }
                logFileRepository.AddFile(logFile);
            }
        }
Пример #2
0
        private void ReadFile()
        {
            Console.WriteLine($"{DateTime.Now} | Checking file");

            if (!File.Exists(path))
            {
                return;
            }

            var createdDate = File.GetCreationTime(path);

            var logFile = logFileRepository.GetFileByName(path);

            if (logFile != null)
            {
                if (createdDate.MongoEquals(logFile.Created))
                {
                    logFile.ReadBytes = 0;
                }

                ReadLines(logFile);
                logFileRepository.UpdateFile(logFile);
            }
            else
            {
                logFile = new LogFile
                {
                    Path      = path,
                    Created   = File.GetCreationTime(path),
                    ReadBytes = 0
                };

                ReadLines(logFile);
                logFileRepository.AddFile(logFile);
            }
        }
Пример #3
0
        private void ReadLinesFromFile()
        {
            _cancellationTokenSource.Cancel();

            ColorConsole.Yellow($"Last read file is \"{_lastFile}\"");
            if (_isTaskFinish)
            {
                _cancellationTokenSource = new CancellationTokenSource();
                _cancellationToken       = _cancellationTokenSource.Token;

                var taskOptions = new TaskOptions
                {
                    Path = _lastFile,
                    Skip = _skip
                };

                Task.Factory.StartNew(x =>
                {
                    ColorConsole.Green("Check new lines in the file");

                    _isThereFileInQueue = false;
                    _isTaskFinish       = false;

                    var options = (TaskOptions)x;

                    while (true)
                    {
                        if (_cancellationToken.IsCancellationRequested && FileHelper.FileLengthInBytes(options.Path) <= options.Skip)
                        {
                            options.Skip  = 0;
                            _isTaskFinish = true;
                            _cancellationToken.ThrowIfCancellationRequested();
                        }

                        var lines = new List <string>();
                        using (var fileStream = new FileStream(options.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            using (var streamReader = new StreamReader(fileStream))
                            {
                                streamReader.BaseStream.Seek(options.Skip, SeekOrigin.Begin);
                                while (streamReader.Peek() >= 0)
                                {
                                    var readLine = streamReader.ReadLine();
                                    if (!string.IsNullOrEmpty(readLine))
                                    {
                                        lines.Add(readLine);
                                        options.Skip = streamReader.BaseStream.Length;
                                        logFileRepository.UpdateFile(new LogFile
                                        {
                                            Path      = options.Path,
                                            ReadBytes = options.Skip
                                        });
                                    }
                                }
                            }
                        }

                        if (lines.Count > 0)
                        {
                            var logs = parsers.ParseLogs(lines);
                            if (logs != null)
                            {
                                ColorConsole.Yellow($"{logs.Count} logs will be added");

                                logRepository.InsertBatch(logs);

                                _lastReadLine = lines.Last();
                            }
                        }

                        ColorConsole.Green($"New Lines were read {lines.Count}");
                        ColorConsole.Green($"Last read line is \"{_lastReadLine}\"");

                        Thread.Sleep(_fileReadNewLinesInterval);
                    }
                }, taskOptions, _cancellationToken);
            }
        }