Esempio n. 1
0
        public void GivenFileSystemEventWatcherShouldExecuteCallback()
        {
            var changedFiles = new List <string>();
            var config       = new FileSystemEventConfiguration(Path.GetTempPath(), $"*.{_tempFileExtension}");

            using (var cts = new CancellationTokenSource())
                using (var watcher = new Watcher(InternalChanger, config, cts.Token))
                {
                    watcher.Watch();

                    var tempFile = CreateTempFile();
                    Thread.Sleep(2000);
                    TryDeleteFile(tempFile);
                    cts.Cancel();
                    Assert.Contains(changedFiles, c => c == tempFile);
                }

            void InternalChanger(FileSystemEventArgs fse)
            {
                lock (changedFiles)
                {
                    changedFiles.Add(fse.FullPath);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Watcher"/> class.
        /// </summary>
        /// <param name="callback">
        /// Callback to execute on new file system event
        /// </param>
        /// <param name="configuration">        Initial configuration object </param>
        /// <param name="cancellationToken">    Cancellation token to signal to stop watching </param>
        /// <param name="logger">               Logger to use </param>
        public Watcher(Action <FileSystemEventArgs> callback, FileSystemEventConfiguration configuration,
                       CancellationToken cancellationToken, ILogger <Watcher> logger = null)
        {
            if (cancellationToken == default)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }

            _callback          = callback;
            _cancellationToken = cancellationToken;
            _configuration     = configuration;
            _logger            = logger ?? NullLogger <Watcher> .Instance;

            Initialize();
        }
        /// <summary>
        /// Initializes a new instance of <see cref="FileSystemEventCollection"/>
        /// </summary>
        /// <param name="configuration">        Configuration to use </param>
        /// <param name="cancellationToken">    Cancellation token to signal to watcher to stop </param>
        /// <param name="logger">               Logger to use </param>
        public FileSystemEventCollection(FileSystemEventConfiguration configuration, CancellationToken cancellationToken,
                                         ILogger <FileSystemEventCollection> logger = null)
        {
            if (cancellationToken == default)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }
            if (configuration is null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            _configuration     = configuration;
            _cancellationToken = cancellationToken;
            _logger            = logger ?? NullLogger <FileSystemEventCollection> .Instance;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="Watcher"/> class.
 /// </summary>
 /// <param name="configuration">     Initial configuration object </param>
 /// <param name="cancellationToken"> Cancellation token to signal to stop watching </param>
 /// <param name="logger">            Logger to use </param>
 public Watcher(FileSystemEventConfiguration configuration, CancellationToken cancellationToken, ILogger <Watcher> logger = null)
     : this(null, configuration, cancellationToken, logger)
 {
 }