@@ -20,7 +20,17 @@ public class LogfileReader : IAutoLogLineColumnizerCallback
2020
2121 private readonly GetLogLineFx _logLineFx ;
2222
23- private readonly string _fileName ;
23+ private string FileName
24+ {
25+ get ; init
26+ {
27+ var uri = new Uri ( value ) ;
28+ if ( uri . IsFile )
29+ {
30+ field = uri . LocalPath ; // Convert the URI to a local file path
31+ }
32+ }
33+ }
2434 private readonly int _MAX_BUFFERS = 10 ;
2535 private readonly int _MAX_LINES_PER_BUFFER = 100 ;
2636
@@ -63,7 +73,7 @@ public LogfileReader(string fileName, EncodingOptions encodingOptions, bool mult
6373 return ;
6474 }
6575
66- _fileName = fileName ;
76+ FileName = fileName ;
6777 EncodingOptions = encodingOptions ;
6878 IsMultiFile = multiFile ;
6979 _MAX_BUFFERS = bufferCount ;
@@ -117,7 +127,7 @@ public LogfileReader(string[] fileNames, EncodingOptions encodingOptions, int bu
117127 }
118128
119129 _watchedILogFileInfo = fileInfo ;
120- _fileName = fileInfo . FullName ;
130+ FileName = fileInfo . FullName ;
121131
122132 StartGCThread ( ) ;
123133 }
@@ -265,7 +275,7 @@ public void ReadFiles()
265275 /// <returns></returns>
266276 public int ShiftBuffers ( )
267277 {
268- _logger . Info ( "ShiftBuffers() begin for {0}{1}" , _fileName , IsMultiFile ? " (MultiFile)" : "" ) ;
278+ _logger . Info ( "ShiftBuffers() begin for {0}{1}" , FileName , IsMultiFile ? " (MultiFile)" : "" ) ;
269279 AcquireBufferListWriterLock ( ) ;
270280 int offset = 0 ;
271281 _isLineCountDirty = true ;
@@ -579,7 +589,7 @@ await Task.Run(() =>
579589 long oldSize = 0 ;
580590 try
581591 {
582- OnLoadingStarted ( new LoadFileEventArgs ( _fileName , 0 , false , 0 , false ) ) ;
592+ OnLoadingStarted ( new LoadFileEventArgs ( FileName , 0 , false , 0 , false ) ) ;
583593 ReadFiles ( ) ;
584594 if ( ! _isDeleted )
585595 {
@@ -593,25 +603,57 @@ await Task.Run(() =>
593603 }
594604 } ) ;
595605
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
606+ try
607+ {
608+ _watcher = new FileSystemWatcher
609+ {
610+ NotifyFilter = //NotifyFilters.Attributes
611+ //| NotifyFilters.CreationTime
612+ //| NotifyFilters.DirectoryName
613+ //|
614+ NotifyFilters . FileName
615+ //| NotifyFilters.LastAccess
616+ | NotifyFilters . LastWrite
617+ //| NotifyFilters.Security
618+ | NotifyFilters . Size ,
619+
620+ Path = Path . GetDirectoryName ( FileName ) ?? throw new ArgumentException ( "Invalid file path" ) ,
621+ Filter = Path . GetFileName ( FileName ) , // Sets filter to the specific
622+ EnableRaisingEvents = true
623+ } ;
624+ }
625+ catch ( UnauthorizedAccessException ex )
626+ {
627+ Console . WriteLine ( $ "Access denied: { ex . Message } ") ;
628+ }
629+ catch ( ArgumentException ex )
630+ {
631+ Console . WriteLine ( $ "Invalid argument: { ex . Message } ") ;
632+ }
633+ catch ( Exception ex )
634+ {
635+ Console . WriteLine ( $ "An error occurred: { ex . Message } ") ;
636+ }
637+
638+ _watcher . Error += ( sender , e ) =>
639+ {
640+ Console . WriteLine ( $ "Error occurred: { e . GetException ( ) . Message } ") ;
641+ Task . Delay ( 5000 ) . ContinueWith ( _ =>
642+ {
643+ try
644+ {
645+ Console . WriteLine ( "Attempting to restart the watcher..." ) ;
646+ _watcher . EnableRaisingEvents = true ;
647+ }
648+ catch ( Exception ex )
649+ {
650+ Console . WriteLine ( $ "Failed to restart the watcher: { ex . Message } ") ;
651+ }
652+ } ) ;
611653 } ;
612654 _watcher . Changed += OnFileChanged ;
613655 _watcher . Created += OnCreated ;
614- _watcher . Deleted += OnFileDeleted ;
656+ _watcher . Deleted += OnFileDeleted ;
615657 _watcher . Renamed += OnFileRenamed ;
616658 _watcher . Error += OnFileError ;
617659
@@ -651,7 +693,8 @@ private void OnFileChanged(object sender, FileSystemEventArgs e)
651693 {
652694 MonitoredFileNotFound ( ) ;
653695 }
654- catch ( Exception ex ) {
696+ catch ( Exception ex )
697+ {
655698 throw new NotImplementedException ( ) ;
656699 }
657700 }
@@ -708,11 +751,11 @@ public void DeleteAllContent()
708751 {
709752 if ( _contentDeleted )
710753 {
711- _logger . Debug ( "Buffers for {0} already deleted." , Util . GetNameFromPath ( _fileName ) ) ;
754+ _logger . Debug ( "Buffers for {0} already deleted." , Util . GetNameFromPath ( FileName ) ) ;
712755 return ;
713756 }
714757
715- _logger . Info ( "Deleting all log buffers for {0}. Used mem: {1:N0}" , Util . GetNameFromPath ( _fileName ) , GC . GetTotalMemory ( true ) ) ; //TODO [Z] uh GC collect calls creepy
758+ _logger . Info ( "Deleting all log buffers for {0}. Used mem: {1:N0}" , Util . GetNameFromPath ( FileName ) , GC . GetTotalMemory ( true ) ) ; //TODO [Z] uh GC collect calls creepy
716759 AcquireBufferListWriterLock ( ) ;
717760 _lruCacheDictLock . AcquireWriterLock ( Timeout . Infinite ) ;
718761 _disposeLock . AcquireWriterLock ( Timeout . Infinite ) ;
@@ -779,7 +822,7 @@ internal void LogBufferInfoForLine(int lineNum)
779822 if ( buffer == null )
780823 {
781824 ReleaseBufferListReaderLock ( ) ;
782- _logger . Error ( "Cannot find buffer for line {0}, file: {1}{2}" , lineNum , _fileName , IsMultiFile ? " (MultiFile)" : "" ) ;
825+ _logger . Error ( "Cannot find buffer for line {0}, file: {1}{2}" , lineNum , FileName , IsMultiFile ? " (MultiFile)" : "" ) ;
783826 return ;
784827 }
785828
@@ -804,7 +847,7 @@ internal void LogBufferDiagnostic()
804847 _lruCacheDictLock . ReleaseReaderLock ( ) ;
805848
806849 AcquireBufferListReaderLock ( ) ;
807- _logger . Info ( "File: {0}\r \n Buffer count: {1}\r \n Disposed buffers: {2}" , _fileName , _bufferList . Count , _bufferList . Count - cacheCount ) ;
850+ _logger . Info ( "File: {0}\r \n Buffer count: {1}\r \n Disposed buffers: {2}" , FileName , _bufferList . Count , _bufferList . Count - cacheCount ) ;
808851 int lineNum = 0 ;
809852 long disposeSum = 0 ;
810853 long maxDispose = 0 ;
@@ -860,7 +903,7 @@ private Task<ILogLine> GetLogLineInternal(int lineNum)
860903 if ( logBuffer == null )
861904 {
862905 ReleaseBufferListReaderLock ( ) ;
863- _logger . Error ( "Cannot find buffer for line {0}, file: {1}{2}" , lineNum , _fileName , IsMultiFile ? " (MultiFile)" : "" ) ;
906+ _logger . Error ( "Cannot find buffer for line {0}, file: {1}{2}" , lineNum , FileName , IsMultiFile ? " (MultiFile)" : "" ) ;
864907 return null ;
865908 }
866909
@@ -1225,7 +1268,7 @@ private void GarbageCollectLruCache()
12251268#if DEBUG
12261269 if ( diff > 0 )
12271270 {
1228- _logger . Info ( "Removing {0} entries from LRU cache for {1}" , diff , Util . GetNameFromPath ( _fileName ) ) ;
1271+ _logger . Info ( "Removing {0} entries from LRU cache for {1}" , diff , Util . GetNameFromPath ( FileName ) ) ;
12291272 }
12301273#endif
12311274 SortedList < long , int > useSorterList = [ ] ;
@@ -1573,7 +1616,7 @@ private void FileChanged()
15731616 long newSize = _fileLength ;
15741617 //if (this.currFileSize != newSize)
15751618 {
1576- _logger . Info ( "file size changed. new size={0}, file: {1}" , newSize , _fileName ) ;
1619+ _logger . Info ( "file size changed. new size={0}, file: {1}" , newSize , FileName ) ;
15771620 FireChangeEvent ( ) ;
15781621 }
15791622 }
@@ -1596,7 +1639,7 @@ private void FireChangeEvent()
15961639 {
15971640 // ReloadBufferList(); // removed because reloading is triggered by owning LogWindow
15981641 // Trigger "new file" handling (reload)
1599- OnLoadFile ( new LoadFileEventArgs ( _fileName , 0 , true , _fileLength , true ) ) ;
1642+ OnLoadFile ( new LoadFileEventArgs ( FileName , 0 , true , _fileLength , true ) ) ;
16001643
16011644 if ( _isDeleted )
16021645 {
0 commit comments