private void ReadFile()
        {
            long fileSize = new FileInfo(FilePath).Length;

            if (fileSize == LastSize)
            {
                return;
            }

            string newText;

            if (fileSize < LastSize)
            {
                // New file was created
                using (var fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using var reader = new StreamReader(fs);
                    newText          = reader.ReadToEnd();
                }

                LastSize         = new FileInfo(FilePath).Length;
                LogContentToSend = new StringBuilder(newText);
            }
            else
            {
                // File got bigger
                using (var fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    long newTextSize = fs.Length - LastSize;
                    var  b           = new byte[newTextSize];

                    fs.Seek(LastSize, SeekOrigin.Begin);
                    fs.Read(b, 0, (int)(newTextSize));

                    newText = Encoding.UTF8.GetString(b);
                }

                LastSize = fileSize;
                LogContentToSend.Append(newText);
            }

            if (InitialLoad == false)
            {
                try
                {
                    OnFileSizeChangedNewText?.Invoke(this, newText);
                }
                catch (Exception ex)
                {
                    Log.Fatal(ex, "Unexpected error:");
                    //System.Diagnostics.Debugger.Break();
                    throw;
                }
            }

            InitialLoad = false;
        }
        private void ReadFile()
        {
            var fileSize = new FileInfo(filePath).Length;

            if (fileSize == lastSize || fileSize == 0)
            {
                return;
            }

            string newText;

            if (fileSize < lastSize)
            {
                // New file was created, start at begin
                lastSize         = 0;
                LogContentToSend = new StringBuilder();
            }

            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var readUntil = fs.Length;
                var readCount = readUntil - lastSize;
                var b         = new byte[readCount];

                fs.Seek(lastSize, SeekOrigin.Begin);
                fs.Read(b, 0, (int)readCount);

                newText = Encoding.UTF8.GetString(b);

                lastSize = readUntil;
                LogContentToSend.Append(newText);
            }

            if (initialLoad == false)
            {
                try
                {
                    OnFileSizeChangedNewText?.Invoke(this, newText);
                }
                catch (Exception ex)
                {
                    Log.Fatal(ex, "Unexpected error:");
                    //System.Diagnostics.Debugger.Break();
                    throw;
                }
            }

            initialLoad = false;
        }
Esempio n. 3
0
        void ReadFile()
        {
            var fileSize = new FileInfo(filePath).Length;

            if (fileSize != lastSize)
            {
                var newText = "";

                if (fileSize < lastSize)
                {
                    // New file was created
                    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        using (StreamReader reader = new StreamReader(fs))
                        {
                            newText = reader.ReadToEnd();
                        }
                    lastSize         = new FileInfo(filePath).Length;
                    LogContentToSend = new StringBuilder(newText);
                }
                else
                {
                    // File got bigger
                    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var    newTextSize = fs.Length - lastSize;
                        byte[] b           = new byte[newTextSize];

                        fs.Seek(lastSize, SeekOrigin.Begin);
                        fs.Read(b, 0, (int)(newTextSize));

                        newText = Encoding.UTF8.GetString(b);
                    }

                    lastSize = fileSize;
                    LogContentToSend.Append(newText);
                }

                if (initialLoad == false)
                {
                    OnFileSizeChangedNewText?.Invoke(this, newText);
                }

                initialLoad = false;
            }
        }