Esempio n. 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);
            }
        }
Esempio n. 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);
            }
        }
Esempio n. 3
0
        private void WatchDirectory()
        {
            ColorConsole.Yellow("Watch directory");

            var allFiles = Directory.GetFiles(logsPath);
            var newFiles = allFiles.Except(logFileRepository.GetFiles().Select(x => x.Path)).ToArray();

            if (!newFiles.Any() && !_isThereFileInQueue)
            {
                if (allFiles.Length != 0 && allFiles.Last() != _lastFile)
                {
                    _lastFile = allFiles.Length > 0 ? allFiles.Last() : _lastFile;

                    var logFile = logFileRepository.GetFileByName(_lastFile);
                    _skip = logFile.ReadBytes;

                    ReadLinesFromFile();
                }
                return;
            }

            ColorConsole.Yellow("There are new files or there is file in queue");
            foreach (var file in newFiles)
            {
                var  lines = new List <string>();
                long position;
                using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (var streamReader = new StreamReader(fileStream))
                    {
                        streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                        while (streamReader.Peek() >= 0)
                        {
                            lines.Add(streamReader.ReadLine());
                        }
                        position = streamReader.BaseStream.Length;
                    }
                }

                _skip = position;

                var logs = parsers.ParseLogs(lines.ToList());
                if (logs != null)
                {
                    ColorConsole.Yellow($"{logs.Count} logs will be added");
                    ColorConsole.Yellow($"Last read lines is {lines.Last()}");
                    logRepository.InsertBatch(logs);
                }

                logFileRepository.AddFile(new LogFile {
                    Path = file, ReadBytes = position
                });
                _isThereFileInQueue = true;
            }

            if (newFiles.Length != 0 && _lastFile != newFiles.Last() || _isThereFileInQueue)
            {
                _lastFile = newFiles.Length > 0 ? newFiles.Last() : _lastFile;

                ReadLinesFromFile();
            }
        }