@@ -20,7 +20,17 @@ public class LogfileReader : IAutoLogLineColumnizerCallback
20
20
21
21
private readonly GetLogLineFx _logLineFx ;
22
22
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
+ }
24
34
private readonly int _MAX_BUFFERS = 10 ;
25
35
private readonly int _MAX_LINES_PER_BUFFER = 100 ;
26
36
@@ -63,7 +73,7 @@ public LogfileReader(string fileName, EncodingOptions encodingOptions, bool mult
63
73
return ;
64
74
}
65
75
66
- _fileName = fileName ;
76
+ FileName = fileName ;
67
77
EncodingOptions = encodingOptions ;
68
78
IsMultiFile = multiFile ;
69
79
_MAX_BUFFERS = bufferCount ;
@@ -117,7 +127,7 @@ public LogfileReader(string[] fileNames, EncodingOptions encodingOptions, int bu
117
127
}
118
128
119
129
_watchedILogFileInfo = fileInfo ;
120
- _fileName = fileInfo . FullName ;
130
+ FileName = fileInfo . FullName ;
121
131
122
132
StartGCThread ( ) ;
123
133
}
@@ -265,7 +275,7 @@ public void ReadFiles()
265
275
/// <returns></returns>
266
276
public int ShiftBuffers ( )
267
277
{
268
- _logger . Info ( "ShiftBuffers() begin for {0}{1}" , _fileName , IsMultiFile ? " (MultiFile)" : "" ) ;
278
+ _logger . Info ( "ShiftBuffers() begin for {0}{1}" , FileName , IsMultiFile ? " (MultiFile)" : "" ) ;
269
279
AcquireBufferListWriterLock ( ) ;
270
280
int offset = 0 ;
271
281
_isLineCountDirty = true ;
@@ -579,7 +589,7 @@ await Task.Run(() =>
579
589
long oldSize = 0 ;
580
590
try
581
591
{
582
- OnLoadingStarted ( new LoadFileEventArgs ( _fileName , 0 , false , 0 , false ) ) ;
592
+ OnLoadingStarted ( new LoadFileEventArgs ( FileName , 0 , false , 0 , false ) ) ;
583
593
ReadFiles ( ) ;
584
594
if ( ! _isDeleted )
585
595
{
@@ -593,25 +603,57 @@ await Task.Run(() =>
593
603
}
594
604
} ) ;
595
605
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
+ } ) ;
611
653
} ;
612
654
_watcher . Changed += OnFileChanged ;
613
655
_watcher . Created += OnCreated ;
614
- _watcher . Deleted += OnFileDeleted ;
656
+ _watcher . Deleted += OnFileDeleted ;
615
657
_watcher . Renamed += OnFileRenamed ;
616
658
_watcher . Error += OnFileError ;
617
659
@@ -651,7 +693,8 @@ private void OnFileChanged(object sender, FileSystemEventArgs e)
651
693
{
652
694
MonitoredFileNotFound ( ) ;
653
695
}
654
- catch ( Exception ex ) {
696
+ catch ( Exception ex )
697
+ {
655
698
throw new NotImplementedException ( ) ;
656
699
}
657
700
}
@@ -708,11 +751,11 @@ public void DeleteAllContent()
708
751
{
709
752
if ( _contentDeleted )
710
753
{
711
- _logger . Debug ( "Buffers for {0} already deleted." , Util . GetNameFromPath ( _fileName ) ) ;
754
+ _logger . Debug ( "Buffers for {0} already deleted." , Util . GetNameFromPath ( FileName ) ) ;
712
755
return ;
713
756
}
714
757
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
716
759
AcquireBufferListWriterLock ( ) ;
717
760
_lruCacheDictLock . AcquireWriterLock ( Timeout . Infinite ) ;
718
761
_disposeLock . AcquireWriterLock ( Timeout . Infinite ) ;
@@ -779,7 +822,7 @@ internal void LogBufferInfoForLine(int lineNum)
779
822
if ( buffer == null )
780
823
{
781
824
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)" : "" ) ;
783
826
return ;
784
827
}
785
828
@@ -804,7 +847,7 @@ internal void LogBufferDiagnostic()
804
847
_lruCacheDictLock . ReleaseReaderLock ( ) ;
805
848
806
849
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 ) ;
808
851
int lineNum = 0 ;
809
852
long disposeSum = 0 ;
810
853
long maxDispose = 0 ;
@@ -860,7 +903,7 @@ private Task<ILogLine> GetLogLineInternal(int lineNum)
860
903
if ( logBuffer == null )
861
904
{
862
905
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)" : "" ) ;
864
907
return null ;
865
908
}
866
909
@@ -1225,7 +1268,7 @@ private void GarbageCollectLruCache()
1225
1268
#if DEBUG
1226
1269
if ( diff > 0 )
1227
1270
{
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 ) ) ;
1229
1272
}
1230
1273
#endif
1231
1274
SortedList < long , int > useSorterList = [ ] ;
@@ -1573,7 +1616,7 @@ private void FileChanged()
1573
1616
long newSize = _fileLength ;
1574
1617
//if (this.currFileSize != newSize)
1575
1618
{
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 ) ;
1577
1620
FireChangeEvent ( ) ;
1578
1621
}
1579
1622
}
@@ -1596,7 +1639,7 @@ private void FireChangeEvent()
1596
1639
{
1597
1640
// ReloadBufferList(); // removed because reloading is triggered by owning LogWindow
1598
1641
// Trigger "new file" handling (reload)
1599
- OnLoadFile ( new LoadFileEventArgs ( _fileName , 0 , true , _fileLength , true ) ) ;
1642
+ OnLoadFile ( new LoadFileEventArgs ( FileName , 0 , true , _fileLength , true ) ) ;
1600
1643
1601
1644
if ( _isDeleted )
1602
1645
{
0 commit comments