Tuesday, March 11, 2008

How to track visitors to your site in ASP.NET & C# - 2

Code to log the IP address & DNS name for visitors to each page
Once you have published a site in ASP.NET, you'd like to know who are your visitors. One way is to check your event log on the host server. Another option is to write your own code. You'd basically like to log the IP address, and DNS name for the visitor, and it would be nice to know which page they are visiting.
To log the ip address using ASP.NET, you can call:
Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
Another usefull variable is
Request.ServerVariables["REMOTE_ADDR"]
A combination of the two can be done as follows:
private string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
return strIpAddress;
}
Next, you'd want to log the DNS name. To do so, you can call:
Dns.GetHostByAddress(string ipAddress)
And log the HostName property returned by the call. Now to log the address of the page being requested, you can call:
Request.Url.ToString()
So to combine all these together, here is the code to build a visitors.log file:
// Track Visitors
string ipAddress = IpAddress();
string hostName = Dns.GetHostByAddress(ipAddress).HostName;
StreamWriter wrtr = new StreamWriter(Server.MapPath("visitors.log"), true);
wrtr.WriteLine(DateTime.Now.ToString() + " " + ipAddress + " " + hostName + " " + Request.Url.ToString());
wrtr.Close();
The next question is what part of your ASP.NET page can you put the above code? There are two possible places, the first would be in the Application.BeginRequest event handler, the second can be in the Master page Load event. Here is how to do it in the Load event:
using System;
using System.Configuration;
using System.Net;
using System.IO;
namespace MyWebSite
{
public partial class DefaultMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// Track Visitors
string ipAddress = IpAddress();
string hostName = Dns.GetHostByAddres(ipAddress).HostName;
StreamWriter wrtr = new StreamWriter(Server.MapPath("visitors.log"), true);
wrtr.WriteLine(DateTime.Now.ToString() + " " + ipAddress + " " + hostName + " " + Request.Url.ToString());
wrtr.Close();
}
private string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
return strIpAddress;
}
}
}
Once you start getting some visitors, the result visitors.log file would look something like this:
4/2/2007 8:47:34 PM 74.6.67.155 lj612164.inktomisearch.com http://www.mycsharpcorner.com/Post.aspx?postID=22
4/2/2007 8:53:05 PM 66.249.66.35 crawl-66-249-66-35.googlebot.com http://www.mycsharpcorner.com/Default.aspx?categoryID=18
4/2/2007 9:02:41 PM 66.249.66.35 crawl-66-249-66-35.googlebot.com http://www.mycsharpcorner.com/Default.aspx
4/2/2007 10:06:20 PM 69.117.147.109 ool-4575936d.dyn.optonline.net http://www.mycsharpcorner.com/Post.aspx?postID=15
4/2/2007 10:12:23 PM 72.30.216.102 lm502014.inktomisearch.com http://www.mycsharpcorner.com/Post.aspx?postID=22
4/2/2007 11:04:24 PM 66.249.66.35 crawl-66-249-66-35.googlebot.com http://www.mycsharpcorner.com/Post.aspx?postID=15
4/2/2007 11:08:22 PM 66.249.66.35 crawl-66-249-66-35.googlebot.com http://www.mycsharpcorner.com/Post.aspx?postID=23
Happy Programming!

How to track visitors to your site in ASP.NET & C#

Once you have published a site in ASP.NET, you'd like to know who are your visitors. One way is to check your event log on the host server. Another option is to write your own code. You'd basically like to log the IP address, and DNS name for the visitor, and it would be nice to know which page they are visiting.
To log the ip address using ASP.NET, you can call:
Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
Another usefull variable is
Request.ServerVariables["REMOTE_ADDR"]
A combination of the two can be done as follows:
private string IpAddress()
{
string strIpAddress;
strIpAddress =
Request.ServerVariables ["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
return strIpAddress;
}
Next, you'd want to log the DNS name. To do so, you can call:
Dns .GetHostByAddress( string ipAddress);
And log the HostName property returned by the call. Now to log the address of the page being requested, you can call:
Request.Url.ToString();
So to combine all these together, here is the code to build a visitors.log file:
// Track Visitors
string ipAddress = IpAddress();
string hostName = Dns .GetHostByAddress(ipAddress).HostName;
StreamWriter wrtr = new StreamWriter( Server.MapPath(
"visitors.log" ), true );
wrtr.WriteLine( DateTime .Now.ToString() + " " + ipAddress
+ " " + hostName + " " + Request.Url.ToString());
wrtr.Close();
The next question is what part of your ASP.NET page can you put the above code? There are two possible places, the first would be in the Application.BeginRequest event handler, the second can be in the Master page Load event. Here is how to do it in the Load event:
using System;
using System.Configuration;
using System.Net;
using System.IO;

namespace MyWebSite
{
public partial class DefaultMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load( object sender, EventArgs e)
{
// Track Visitors
string ipAddress = IpAddress();
string hostName = Dns .GetHostByAddres(ipAddress).HostName;
StreamWriter wrtr = new StreamWriter (Server.MapPath( "visitors.log" ), true );
wrtr.WriteLine( DateTime .Now.ToString() + " " + ipAddress + " " + hostName + " " + Request.Url.ToString());
wrtr.Close();
}
private string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables[ "HTTP_X_FORWARDED_FOR" ];
if (strIpAddress == null )
strIpAddress = Request.ServerVariables[ "REMOTE_ADDR" ];
return strIpAddress;
}
}
}
Once you start getting some visitors, the result visitors.log file would look something like this:4/2/20078:47:34 PM 74.6.67.155 lj612164.inktomisearch.com http://www.mycsharpcorner.com/Post.aspx?postID=22
4/2/20078:53:05 PM 66.249.66.35 crawl-66-249-66-35.googlebot.com http://www.mycsharpcorner.com/Default.aspx?categoryID=18
4/2/20079:02:41 PM 66.249.66.35 crawl-66-249-66-35.googlebot.com http://www.mycsharpcorner.com/Default.aspx
4/2/200710:06:20 PM 69.117.147.109 ool-4575936d.dyn.optonline.net http://www.mycsharpcorner.com/Post.aspx?postID=15
4/2/200710:12:23 PM 72.30.216.102 lm502014.inktomisearch.com http://www.mycsharpcorner.com/Post.aspx?postID=22
4/2/200711:04:24 PM 66.249.66.35 crawl-66-249-66-35.googlebot.com http://www.mycsharpcorner.com/Post.aspx?postID=15
4/2/200711:08:22 PM 66.249.66.35 crawl-66-249-66-35.googlebot.com http://www.mycsharpcorner.com/Post.aspx?postID=23
And now you can know who comes to visit your ASP.NET Page.