Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

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/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.
kick it on DotNetKicks.com

Feedback

Posted on 1/13/2010 1:47:38 AM

Very good solution.......Thanks....

Posted on 2/16/2009 9:50:28 AM

I think its not working in .net 2.0 ,i am getting local ip of server after place code in sever,i dont know why ?

Posted on 12/6/2007 3:42:07 AM

did you give write permissions for your asp.net-workerprocess in this directory and file?

Posted on 9/20/2007 2:48:09 AM

hi
its good
but any one can help me regarding the following projetc
Web Traffic Analysis.
I neet it urgentaly.

Posted on 9/20/2007 2:47:34 AM

hi
its good
but any one can help me regarding the following projetc
Web Traffic Analysis.
I neet it urgentaly.

Posted on 6/5/2007 2:39:34 AM

This is not a scalable solution; I have been getting the same error as well. I think it has to do with multiple requests made at the same time to the site, so one request is locking the file to log a certain visitor, the next request arrives and tries to open the file to write as well... normal file locking issue.

One thing to do is to replace the log text file with a database. I think you will get rid of any file locking issues then.

Posted on 6/4/2007 9:33:12 PM

Hi,

This is very useful thanks. One problem, I keep getting an error saying the file is locked.... Any ideas how to fix this? I tried .Flush, .Close, .Dispose and a Using block but it keeps locking the file....

Cheers

Ben

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author