SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers

Website Monitoring Tool to Ping Website in Visual Studio 2013

In this Microsoft .NET tutorial, I'll share C# codes developed in Visual Studio 2013 of a simple website monitoring tool to ping website for its status. You can build your website monitoring software using Visual Studio 2013 with the given C-Sharp codes in this tutorial.

In your the sample website monitoring application requests a URL from the target website which is wanted to be checked for its online status. You can later automate the execution of this application using a scheduler application like I will do in this VS2013 tutorial using Windows 7 Task Scheduler. Using Task Scheduler enables developers to check website status 24 hours and inform a responsible when ping website request fails to respond to possible problems immediately.

Before developers start developing sample website monitoring tool of their own, download Visual Studio 2013 free editions or trial edition.

Open Visual Studio 2013, follow menu option File > New > Project... to choose a project template for the following development task.

create new project in Visual Studio 2013

I prefer Console Application for this tutorial, you can either choose Windows Forms Application project template too.

Visual Studio project template for website monitor tool

Give a name to your Visual Studio 2013 project. I used WebsiteMonitoringTool as the name of this sample project. Define the file folder where you want your application's source will be placed. Mark the Create directory for solution checkmark and then press OK button to let Visual Studio create a blank console application solution for you.

First of all, to log status data on a text file on a local folder let's create a Log method. To write to text file using .NET Framework, developers require a reference to System.IO namespace for using TextWriter class. Add following namespace reference at the beginning of your project's source codes.

using System.IO;

Following static method can be used to log simple messages on text file located on a local folder.
Of course, for more professional web site monitoring software, these log method can be overloaded with more input parameter for providing more detailed information to the users.

For the simplicity of this Visual Studio 2013 tutorial for a basic web site health checker tool, I plan to pass only logMessage as 'OK' and 'FAIL'. If website URL request is successfull, as the response returns HttpStatusCode.OK code, then log will write OK in the web site pinger log file. Otherwise if HTTP Status Code is not OK, then the application will write FAIL into the log file.

Of course, you can add additional .NET code in a TRY CATCH block and pass the Web Exception or the Exception class instance to the log method and write exception message and other details on the log file.

static string fileName = "c:\\logs\\ping-website.txt";

public static void Log(string logMessage)
 TextWriter logWriter = File.AppendText(fileName);
 logWriter.WriteLine(DateTime.Now.ToLongTimeString() + ' ' + logMessage);

Let's continue with a secondary function for website check application which enables sending emails to a list of recepients in case the website ping fails.

Because for sending email from .NET application, we need to use MailMessage class.
So add the namespace reference for System.Net.Mail namespace in your project references.

using System.Net.Mail;

And below I copy the C# code which can be used to send email to previously set email recipient list.

public static void SendEmail()
 MailMessage mail = new MailMessage("from@yoursite.com", "to@yoursite.com");
 SmtpClient client = new SmtpClient();
 client.Port = 25;
 client.DeliveryMethod = SmtpDeliveryMethod.Network;
 client.UseDefaultCredentials = false;
 client.Host = "smtp.yoursite.com";
 mail.Subject = "Web Site Monitoring Tool Alert";
 mail.Body = "Web site check status fail: " + DateTime.Now.ToString();

After creating secondary methods, we are now ready to continue with main website checker code. We will ping website using HttpWebRequest class and HttpWebResponse class, so we need a namespace reference in our project. You can add a reference to System.Net namespace in your Visual Studio 2013 console application project

using System.Net;

And here is the C-Sharp codes for sending an HTTP Request for a web URL address and checking the response status code for the target web site health.

static void Main(string[] args)
 //string url = args[0]; // URL address
 string url = "http://www.kodyaz.com";

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 request.Timeout = 120000; // 60 seconds

 // for proxy authentication
 request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

  using (WebResponse response = (HttpWebResponse)request.GetResponse())
   if (((System.Net.HttpWebResponse)(response)).StatusCode.Equals(System.Net.HttpStatusCode.OK))
 catch (Exception)

Please note that if Visual Studio develpers want to pass URL parameter as an argument, they can use the args[] array. For example if you want to use this web site monitoring tool for more than one web address, you can pass the URL to ping as an argument to the application while executing it. The Main() method of the web monitor project will receive these argument parameters and you can set parameters like URL to monitor, or log file full path, etc in this way.

Again for simplicty, I just used a static web URL, but also commented the C# code to read the first parameter from the execution arguments.

Sample Visual Studio 2013 project for web site monitoring application is ready for first run. Build the VS project, following Build > Build Solution menu options and start the program pressing F5 key or Start button on the menu bar.

If you did not get any errors during build process or during the running of your application, you can directly control the log file to see the first outcome from your website ping application.

If you have some problems, you can place breakpoints within the Visual Studio project codes to debug and find the reason for the problem or error.

After developers make their website monitoring software work successfully, the following step will be scheduling this application using a scheduler application. In the next tutorial, I'll show developers how to schedule this sample website ping tool using Windows Task Scheduler tool and check the website health and status of a URL address periodically.

Visual Studio

Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.