@@ -17,9 +17,9 @@ public class FileSystemWatcherEx : IDisposable, IFileSystemWatcherEx
17
17
private EventProcessor ? _processor ;
18
18
private readonly BlockingCollection < FileChangedEvent > _fileEventQueue = new ( ) ;
19
19
20
- private FileWatcher ? _watcher ;
21
- private IFileSystemWatcherWrapper ? _fsw ;
20
+ private SymlinkAwareFileWatcher ? _watcher ;
22
21
private Func < IFileSystemWatcherWrapper > ? _fswFactory ;
22
+ private readonly Action < string > _logger ;
23
23
24
24
// Define the cancellation token.
25
25
private CancellationTokenSource ? _cancelSource ;
@@ -86,6 +86,7 @@ public string Filter
86
86
87
87
#endregion
88
88
89
+
89
90
#region Public Events
90
91
91
92
/// <summary>
@@ -134,11 +135,13 @@ public string Filter
134
135
/// Initialize new instance of <see cref="FileSystemWatcherEx"/>
135
136
/// </summary>
136
137
/// <param name="folderPath"></param>
137
- public FileSystemWatcherEx ( string folderPath = "" )
138
+ /// <param name="logger">Optional Action to log out library internals</param>
139
+ public FileSystemWatcherEx ( string folderPath = "" , Action < string > ? logger = null )
138
140
{
139
141
FolderPath = folderPath ;
142
+ _logger = logger ?? ( _ => { } ) ;
140
143
}
141
-
144
+
142
145
143
146
/// <summary>
144
147
/// Start watching files
@@ -233,10 +236,10 @@ void InvokeRenamedEvent(object? sender, FileChangedEvent fileEvent)
233
236
}
234
237
} , ( log ) =>
235
238
{
236
- Console . WriteLine ( string . Format ( "{0} | {1}" , Enum . GetName ( typeof ( ChangeType ) , ChangeType . LOG ) , log ) ) ;
239
+ Console . WriteLine ( $ " { Enum . GetName ( typeof ( ChangeType ) , ChangeType . LOG ) } | { log } " ) ;
237
240
} ) ;
238
241
239
- _cancelSource = new ( ) ;
242
+ _cancelSource = new CancellationTokenSource ( ) ;
240
243
_thread = new Thread ( ( ) => Thread_DoingWork ( _cancelSource . Token ) )
241
244
{
242
245
// this ensures the thread does not block the process from terminating!
@@ -247,40 +250,32 @@ void InvokeRenamedEvent(object? sender, FileChangedEvent fileEvent)
247
250
248
251
249
252
// Log each event in our special format to output queue
250
- void onEvent ( FileChangedEvent e )
253
+ void OnEvent ( FileChangedEvent e )
251
254
{
252
255
_fileEventQueue . Add ( e ) ;
253
256
}
254
257
255
258
256
- // OnError
257
- void onError ( ErrorEventArgs e )
259
+ void OnError ( ErrorEventArgs e )
258
260
{
259
261
if ( e != null )
260
262
{
261
- OnError ? . Invoke ( this , e ) ;
263
+ this . OnError ? . Invoke ( this , e ) ;
262
264
}
263
265
}
264
266
265
-
266
- // Start watcher
267
- _watcher = new FileWatcher ( ) ;
268
-
269
- _fsw = _watcher . Create ( FolderPath , onEvent , onError , FileSystemWatcherFactory ) ;
270
-
271
- foreach ( var filter in Filters )
267
+ _watcher = new SymlinkAwareFileWatcher ( FolderPath , OnEvent , OnError , FileSystemWatcherFactory , _logger )
272
268
{
273
- _fsw . Filters . Add ( filter ) ;
274
- }
275
-
276
- _fsw . NotifyFilter = NotifyFilter ;
277
- _fsw . IncludeSubdirectories = IncludeSubdirectories ;
278
- _fsw . SynchronizingObject = SynchronizingObject ;
279
-
280
- // Start watching
281
- _fsw . EnableRaisingEvents = true ;
269
+ NotifyFilter = NotifyFilter ,
270
+ IncludeSubdirectories = IncludeSubdirectories ,
271
+ SynchronizingObject = SynchronizingObject ,
272
+ EnableRaisingEvents = true
273
+ } ;
274
+ Filters . ToList ( ) . ForEach ( filter => _watcher . Filters . Add ( filter ) ) ;
275
+ _watcher . Init ( ) ;
282
276
}
283
277
278
+
284
279
internal void StartForTesting (
285
280
Func < string , FileAttributes > getFileAttributesFunc ,
286
281
Func < string , DirectoryInfo [ ] > getDirectoryInfosFunc )
@@ -297,12 +292,6 @@ internal void StartForTesting(
297
292
/// </summary>
298
293
public void Stop ( )
299
294
{
300
- if ( _fsw != null )
301
- {
302
- _fsw . EnableRaisingEvents = false ;
303
- _fsw . Dispose ( ) ;
304
- }
305
-
306
295
_watcher ? . Dispose ( ) ;
307
296
308
297
// stop the thread
@@ -316,14 +305,12 @@ public void Stop()
316
305
/// </summary>
317
306
public void Dispose ( )
318
307
{
319
- _fsw ? . Dispose ( ) ;
320
308
_watcher ? . Dispose ( ) ;
321
309
_cancelSource ? . Dispose ( ) ;
322
310
GC . SuppressFinalize ( this ) ;
323
311
}
324
312
325
313
326
-
327
314
private void Thread_DoingWork ( CancellationToken cancelToken )
328
315
{
329
316
while ( true )
@@ -342,6 +329,5 @@ private void Thread_DoingWork(CancellationToken cancelToken)
342
329
}
343
330
}
344
331
}
345
-
346
-
347
332
}
333
+
0 commit comments