Wednesday, October 30, 2013

Simple Website Monitor with C#

I was recently asked to evaluate some Website monitoring tools for a small client of mine.  While there is no shortage of Website monitoring services available, ranging from free to infinitely elaborate and expensive (and sometimes worth it, depending on the company's size and needs), I ultimately decided to just build one from scratch.  This way, the client would not be at the mercy of SaaS outages, and would be fully in control with the code on-premise.  This solution can be expanded as needed with a UI and possibly a database back-end, but following KISS, I took a simple approach, using a console app with settings in app.config, running it using a scheduled task.  Here are the details:

1. Create a new Console Application project.

2. Using the project's Settings file, create a setting called "SitesToMonitor" of type System.Collections.Specialized.StringCollection.  This allows you to enter a list of strings, using the ellipsis in the Value column.  The list is saved to the app.config file as an xml ArrayOfString, and can be easily accessed, strongly typed, from your C# code. Enter the list of urls you want to monitor. The resulting config item looks like the following:

3. Create a class with a single method accepting a StringCollection, which iterates through them, attempting to retrieve a response from each, along with a method to do whatever your standard logging is when a site returns a negative status code or throws an exception:

4. Add a single line of code to Program.Main to kick off the process:

5. Create a scheduled task to periodically kick it off, and voila!, you have your very own simple, easy-to-configure Website monitor.

3 comments:

  1. Hi Neil. Simple is great. Keeping with the idea of simple, with a single line of code you could push that exception to a cloud logging platform, like Loggr.net, and get sharing of that exception with your team, notifications, and analytics. You could also log the exceptions from all your monitoring programs and get a high-level view of your entire infrastructure. Thanks for the article!

    ReplyDelete
  2. A good source.
    May I save on my blog?

    ReplyDelete
  3. Yes, kay, go ahead! Please link back, though.

    ReplyDelete