Extending Warden – new watcher

In today’s post, I’d like to present how easy it is to create a custom watcher that can be added to the Warden instance and integrated with the whole monitoring process.
Let’s not waste any more time and jump directly into the code.


Actually, the one and only important thing is the IWatcher interface that provides a contract for all of the available watchers:

The interface per se is rather straightforward – by convention each watcher has a default name that can be changed via configuration (e.g. the WebWatcher has a name Web Watcher and so on) and the ExecuteAsync() method returns a custom implementation of the IWatcherCheckResult (so again, for the WebWatcher it would be the WebWatcherCheckResult that derives from the base WatcherCheckResult type).

Let’s try to create a simplistic watcher that will check if a specific process is running.
We will begin with creating a new .NET Core Class Library and naming it ProcessWatcher.
Next, install the base Warden package by running the following command in the Package Manager Console: Install-Package Warden.
We can get rid of default Class1.cs file and add the ProcessWatcherCheckResult class that derives from WatcherCheckResult first.

Having that custom result type, we can go further and finally create a new class called ProcessWatcher that implements the IWatcher interface.

Before we implement the actual functionality of the process watcher, let’s create a console application to check if it’s possible to use this new watcher (you can also create a test project for that case). Select the .NET Core Console Application, name it ProcessWatcher.App, install the Warden library using NuGet and add the reference to the ProcessWatcher class library.
Once it’s done, add the following code to the console app to see if it works correctly:

If you run the application now, you will be able to see the following console output Process watcher check has completed. displayed in a new line every 5 seconds – that’s a good sign, seems that everything works, and the only thing that’s left is the actual process monitoring. Before we do that, we can tweak our result type first:

Now the watcher type is strongly typed and the result class contains additional property that can be used e.g. for some data processing.
Eventually, we can implement the process watcher logic, just make sure that you install the System.Diagnostics.Process package first:

And the final step – fix the console application, by updating these 2 lines:

Run the application now and you shall receive a much more informative description about the process monitoring status. It’s checking whether the MongoDB instance is running, but feel free to use any other process name.

And that’s how easy it is to create your own watcher – all of the official watchers, integrations and Warden configuration options can be used along with the brand new process watcher.
You can download the source code for this example by clicking here.

2 Comments Extending Warden – new watcher

Leave A Comment

Your email address will not be published. Required fields are marked *