In today’s post I’m going to briefly describe one of the latest watchers designed especially for the disk & file monitoring.
If you’re looking for a new plugin to the Warden that will allow you to ensure e.g. that all of required files are available – you’ve found the right place.
As usual, let’s start with the example, yet this one will be a bit more sophisticated than usual:
1 2 3 4 5 6 7 8 |
var configuration = DiskWatcherConfiguration .Create() .WithFilesToCheck(@"D:\Test\File1.txt", @"D:\Test\File2.txt") .WithPartitionsToCheck("D", @"E:\") .WithDirectoriesToCheck(@"D:\Test") .EnsureThat(check => check.FreeSpace > 100000000) .Build(); var diskWatcher = DiskWatcher.Create(configuration); |
In the example above, we want to check the following: 2 files, 2 partitions, 1 directory and also ensure that the disk has at least 95MB of a free space.
As you can see, the disk watcher has quite a few options available, such as:
- WithPartitionsToCheck() – one or more partitions to check e.g. “C:\”
- WithDirectoriesToCheck() – one or more directories to check e.g. “C:\Files”.
- WithFilesToCheck() – one or more files to check e.g. “C:\Files\Log.txt”.
Surely, we can also configure this watcher by using the available extension methods:
1 2 3 4 5 6 7 8 9 10 |
var wardenConfiguration = WardenConfiguration .Create() .AddDiskWatcher(cfg => { cfg.WithFilesToCheck(@"D:\Test\File1.txt", @"D:\Test\File2.txt") .WithPartitionsToCheck("D", @"E:\") .WithDirectoriesToCheck(@"D:\Test") .EnsureThat(check => check.FreeSpace > 100000000) }) //Configure other watchers, hooks etc. |
The result object that you can use inside the EnsureThat() and EnsureThatAsync() methods is quite interesting though. Actually, it contains a lot of the informative data that will help you the validate the performed check, but I’m not going to paste that wall of code here (click on the link in the end if you would like to learn more about it).
To install the package, which is currently in the prerelease version, run the following command in the Package Manager:
1 |
Install-Package Warden.Watchers.Disk -Pre |
If you would like to provide a custom configuration, the following interface is ready to be implemented:
1 2 3 4 5 |
public interface IDiskChecker { Task<DiskCheck> CheckAsync(IEnumerable<string> partitions = null, IEnumerable<string> directories = null, IEnumerable<string> files = null); } |
The default DiskChecker uses the System.IO under the hood.
In order to find out more about the DiskCheck type and the watcher itself, please click here.