Sunday 26 December 2010

Log Visitors details using c#

Recently I have developed one application for the automation. It was web application.
I wanted to track the visitor visited or used my application and then store all the visitors information in "*.csv" file.
For that purpose I have used below code :

try
{
string strVisitorLogLocation=”C://VisitorsLog.csv” ;
string strIpAddress =Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}

string hostName = Dns.GetHostByAddress(strIpAddress).HostName;
StreamWriter wrtr = new StreamWriter(strVisitorLogLocation, true);
wrtr.WriteLine(DateTime.Now.ToString() + "," + strIpAddress + "," + hostName + "," + Request.Url.ToString());
wrtr.Close();
}

catch (Exception ex)
{
//
}

you can use this snipet of the code to get the visitor details like hostname,ipaddress of visitor machine and which all pages he is visited on you application. Thanks!!
~jawed.

No comments: