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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<applicationSettings> | |
<My.SiteMonitor.Console.Properties.Settings> | |
<setting name="SitesToMonitor" serializeAs="Xml"> | |
<value> | |
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> | |
<string>http://somelocalsite/index.aspx</string> | |
<string>http://anothersite/Home</string> | |
<string>http://www.ourpublicsite.com/Main</string> | |
</ArrayOfString> | |
</value> | |
</setting> | |
</My.SiteMonitor.Console.Properties.Settings> | |
</applicationSettings> |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class SiteMonitor | |
{ | |
internal static void PingUrls(StringCollection urlsToPing) | |
{ | |
foreach (string url in urlsToPing) | |
{ | |
try | |
{ | |
var webRequest = HttpWebRequest.Create(url) as HttpWebRequest; | |
webRequest.Method = WebRequestMethods.Http.Get; | |
using (var webResponse = webRequest.GetResponse() as HttpWebResponse) | |
{ | |
if (webResponse.StatusCode != HttpStatusCode.OK) | |
{ | |
throw new WebException(string.Format("The response returned error code {0}: {1}", webResponse.StatusCode, webResponse.StatusDescription)); | |
} | |
} | |
} | |
catch (WebException webException) | |
{ | |
LogWebException(url, webException); | |
} | |
} | |
} | |
private static void LogWebException(string url, WebException webException) | |
{ | |
//TODO: Log, Notify, etc. | |
} | |
} |
4. Add a single line of code to Program.Main to kick off the process:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
SiteMonitor.PingUrls(Settings.Default.SitesToMonitor); | |
} | |
} |
5. Create a scheduled task to periodically kick it off, and voila!, you have your very own simple, easy-to-configure Website monitor.