Skip to content

Commit 8ddd319

Browse files
Randall FlaggRandall Flagg
authored andcommitted
Removed the thread that watches file changes
1 parent 4a7fcd0 commit 8ddd319

File tree

1 file changed

+92
-82
lines changed

1 file changed

+92
-82
lines changed

src/LogExpert/Classes/Log/LogfileReader.cs

Lines changed: 92 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using LogExpert.Entities;
33
using LogExpert.Entities.EventArgs;
44
using LogExpert.Interface;
5-
65
using NLog;
7-
86
using System;
97
using System.Collections.Generic;
108
using System.IO;
@@ -39,7 +37,6 @@ public class LogfileReader : IAutoLogLineColumnizerCallback
3937
private long _fileLength;
4038

4139
private Task _garbageCollectorTask;
42-
private Task _monitorTask;
4340
private readonly CancellationTokenSource cts = new();
4441

4542
private bool _isDeleted;
@@ -51,7 +48,7 @@ public class LogfileReader : IAutoLogLineColumnizerCallback
5148

5249
private ReaderWriterLock _lruCacheDictLock;
5350

54-
51+
private FileSystemWatcher _watcher;
5552
private bool _shouldStop;
5653
private ILogFileInfo _watchedILogFileInfo;
5754

@@ -571,28 +568,109 @@ public int GetRealLineNumForVirtualLineNum(int lineNum)
571568
return result;
572569
}
573570

574-
public void StartMonitoring()
571+
public async Task StartMonitoring()
575572
{
576-
_logger.Info("startMonitoring()");
577-
_monitorTask = Task.Run(MonitorThreadProc, cts.Token);
573+
_logger.Info("StartMonitoring() for file ${_watchedILogFileInfo.FullName}");
574+
575+
await Task.Run(() =>
576+
{
577+
_logger.Info("MonitorThreadProc() for file {0}", _watchedILogFileInfo.FullName);
578+
579+
long oldSize = 0;
580+
try
581+
{
582+
OnLoadingStarted(new LoadFileEventArgs(_fileName, 0, false, 0, false));
583+
ReadFiles();
584+
if (!_isDeleted)
585+
{
586+
oldSize = _fileLength;
587+
OnLoadingFinished();
588+
}
589+
}
590+
catch (Exception e)
591+
{
592+
_logger.Error(e);
593+
}
594+
});
595+
596+
_watcher = new FileSystemWatcher
597+
{
598+
NotifyFilter = //NotifyFilters.Attributes
599+
//| NotifyFilters.CreationTime
600+
//| NotifyFilters.DirectoryName
601+
//|
602+
NotifyFilters.FileName
603+
//| NotifyFilters.LastAccess
604+
| NotifyFilters.LastWrite
605+
//| NotifyFilters.Security
606+
| NotifyFilters.Size,
607+
608+
Path = Path.GetDirectoryName(_fileName) ?? throw new ArgumentException("Invalid file path"),
609+
Filter = Path.GetFileName(_fileName), // Sets filter to the specific
610+
EnableRaisingEvents = true
611+
};
612+
_watcher.Changed += OnFileChanged;
613+
_watcher.Created += OnCreated;
614+
_watcher.Deleted += OnFileDeleted;
615+
_watcher.Renamed += OnFileRenamed;
616+
_watcher.Error += OnFileError;
617+
578618
_shouldStop = false;
579619
}
580620

621+
private void OnFileError(object sender, ErrorEventArgs e)
622+
{
623+
throw new NotImplementedException();
624+
}
625+
626+
private void OnFileRenamed(object sender, RenamedEventArgs e)
627+
{
628+
throw new NotImplementedException();
629+
}
630+
631+
private void OnCreated(object sender, FileSystemEventArgs e)
632+
{
633+
//TODO: This should be deleted before merge?
634+
throw new NotImplementedException();
635+
}
636+
637+
private void OnFileDeleted(object sender, FileSystemEventArgs e)
638+
{
639+
MonitoredFileNotFound();
640+
}
641+
642+
private void OnFileChanged(object sender, FileSystemEventArgs e)
643+
{
644+
try
645+
{
646+
_watchedILogFileInfo.FileHasChanged();
647+
_fileLength = _watchedILogFileInfo.Length;
648+
FileChanged();
649+
}
650+
catch (FileNotFoundException ex)
651+
{
652+
MonitoredFileNotFound();
653+
}
654+
catch (Exception ex) {
655+
throw new NotImplementedException();
656+
}
657+
}
658+
581659
public void StopMonitoring()
582660
{
583661
_logger.Info("stopMonitoring()");
584662
_shouldStop = true;
585663

586-
Thread.Sleep(_watchedILogFileInfo.PollInterval); // leave time for the threads to stop by themselves
587-
588-
if (_monitorTask != null)
664+
if (_watcher != null)
589665
{
590-
if (_monitorTask.Status == TaskStatus.Running) // if thread has not finished, abort it
591-
{
592-
cts.Cancel();
593-
}
666+
_watcher.EnableRaisingEvents = false; // Stop watching
667+
_watcher.Dispose(); // Release resources
668+
_watcher = null; // Clear the reference
669+
594670
}
595671

672+
Thread.Sleep(_watchedILogFileInfo.PollInterval); // leave time for the threads to stop by themselves
673+
596674
if (_garbageCollectorTask.IsCanceled == false)
597675
{
598676
if (_garbageCollectorTask.Status == TaskStatus.Running) // if thread has not finished, abort it
@@ -1464,74 +1542,6 @@ private LogBuffer GetFirstBufferForFileByLogBuffer(LogBuffer logBuffer)
14641542
return resultBuffer;
14651543
}
14661544

1467-
private void MonitorThreadProc()
1468-
{
1469-
Thread.CurrentThread.Name = "MonitorThread";
1470-
//IFileSystemPlugin fs = PluginRegistry.GetInstance().FindFileSystemForUri(this.watchedILogFileInfo.FullName);
1471-
_logger.Info("MonitorThreadProc() for file {0}", _watchedILogFileInfo.FullName);
1472-
1473-
long oldSize = 0;
1474-
try
1475-
{
1476-
OnLoadingStarted(new LoadFileEventArgs(_fileName, 0, false, 0, false));
1477-
ReadFiles();
1478-
if (!_isDeleted)
1479-
{
1480-
oldSize = _fileLength;
1481-
OnLoadingFinished();
1482-
}
1483-
}
1484-
catch (Exception e)
1485-
{
1486-
_logger.Error(e);
1487-
}
1488-
1489-
while (!_shouldStop)
1490-
{
1491-
try
1492-
{
1493-
int pollInterval = _watchedILogFileInfo.PollInterval;
1494-
//#if DEBUG
1495-
// if (_logger.IsDebug)
1496-
// {
1497-
// _logger.logDebug("Poll interval for " + this.fileName + ": " + pollInterval);
1498-
// }
1499-
//#endif
1500-
Thread.Sleep(pollInterval);
1501-
}
1502-
catch (Exception e)
1503-
{
1504-
_logger.Error(e);
1505-
}
1506-
1507-
if (_shouldStop)
1508-
{
1509-
return;
1510-
}
1511-
1512-
try
1513-
{
1514-
if (_watchedILogFileInfo.FileHasChanged())
1515-
{
1516-
_fileLength = _watchedILogFileInfo.Length;
1517-
if (_fileLength == -1)
1518-
{
1519-
MonitoredFileNotFound();
1520-
}
1521-
else
1522-
{
1523-
oldSize = _fileLength;
1524-
FileChanged();
1525-
}
1526-
}
1527-
}
1528-
catch (FileNotFoundException)
1529-
{
1530-
MonitoredFileNotFound();
1531-
}
1532-
}
1533-
}
1534-
15351545
private void MonitoredFileNotFound()
15361546
{
15371547
long oldSize;

0 commit comments

Comments
 (0)