Пример #1
0
        private void ReadLog()
        {
            var newLogFile = Logging.LogFile;

            if (newLogFile != _currentLogFile)
            {
                _currentOffset      = 0;
                _currentLogFile     = newLogFile;
                _currentLogFileName = Logging.LogFileName;
            }

            try
            {
                if (!File.Exists(newLogFile))
                {
                    Title = $@"{this.GetWindowStringValue(@"Title")}";
                    return;
                }
                using var reader = new StreamReader(new FileStream(newLogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                if (_currentOffset == 0)
                {
                    var maxSize = reader.BaseStream.Length;
                    if (maxSize > MaxReadSize)
                    {
                        reader.BaseStream.Seek(-MaxReadSize, SeekOrigin.End);
                        reader.ReadLine();
                    }
                }
                else
                {
                    reader.BaseStream.Seek(_currentOffset, SeekOrigin.Begin);
                }

                var txt = reader.ReadToEnd();
                if (!string.IsNullOrEmpty(txt))
                {
                    LogTextBox.AppendText(txt);

                    if (LogTextBox.SelectionStart == 0 || LogTextBox.IsScrolledToEnd())
                    {
                        LogTextBox.ScrollToEnd();
                    }
                    else
                    {
                        LogTextBox.ScrollToLine(LogTextBox.GetLineIndexFromCharacterIndex(LogTextBox.SelectionStart));
                    }
                }

                _currentOffset = reader.BaseStream.Position;
            }
            catch (FileNotFoundException)
            {
            }
            catch (ArgumentNullException)
            {
            }

            Title = $@"{this.GetWindowStringValue(@"Title")} {_currentLogFileName}";
        }