A few years ago I had a little HttpModule which logged every request to the site as well as worked out what country the request came from (using this code). Anyway just wrote a new version of it and hooked it up to this site...hence the 'Hello New Zealand' title. Always interesting to know where people who read this site are from...oh and the primary purpose, helps me track down errors with requests...just got the Google readers hooked back up by pointing the old RSS feed address at the new feed!

For anyone that's interested, module's code is below...non-optimized but it does the job (oh, and swallows an exception...NEVER do this!). Essentially it's a standard little HttpModule which Asynchronously logs user requests...there's some issues which I'll get round to fixing eventually e.g, it shouldn't use a static to hold the IP table really, should use a Cache object which can be destroyed to save memory...also the DB insertion code uses a really hacky pattern to make my life easier...

 

UPDATE: Just noticed the code formatter I'm using is nasty! Hmm...have to look out the better one I knew about a while ago...

UPDATE2: Found it! http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/ 

Before:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Configuration;
using System.Data.SqlClient;

namespace ML
{
    class TrackingModule : IHttpModule
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["subtextData"].ConnectionString;


        static IPCountryTable table;
        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);

        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = System.Web.HttpContext.Current;

            LogRequestDelegate ld = new LogRequestDelegate(LogRequest);
            AsyncCallback cb = new AsyncCallback(EndLog);
            ld.BeginInvoke(context, cb, ld);
           
        }

        public delegate void LogRequestDelegate(HttpContext context);

        public void LogRequest(HttpContext context)
        {
            if (context == null)
                return;
            if (!context.Request.Path.Contains(".aspx"))
                return;

            if (table == null)
            {
                string resourcePath = context.Server.MapPath(@"~\resources\");
                table = new IPCountryTable(16);
                table.LoadStatisticsFile(Path.Combine(resourcePath, "ripencc.latest"), true);
                table.LoadStatisticsFile(Path.Combine(resourcePath, "arin.latest"), true);
                table.LoadStatisticsFile(Path.Combine(resourcePath, "apnic.latest"), true);
                table.LoadStatisticsFile(Path.Combine(resourcePath, "lacnic.latest"), true);
            }


            string country = !string.IsNullOrEmpty(context.Request.UserHostAddress) ?  table.GetCountry(context.Request.UserHostAddress) : string.Empty;
            string requestUrl = context.Request.RawUrl;
            string userAgent = context.Request.UserAgent;
            string referrer = context.Request.UrlReferrer == null ? null : context.Request.UrlReferrer.ToString() ;
            string userIP = context.Request.UserHostAddress;
            string userHostName = System.Net.Dns.GetHostEntry(userIP).HostName;
            using(SqlConnection sconn = new SqlConnection(connectionString))
        {
            sconn.Open();
            using (SqlCommand scomm = new SqlCommand("INSERT INTO mostlylucid_Tracking (Country, RawUrl, UserAgent, UserReferrer, UserHostAddress, UserHostName) VALUES (@Country, @RawUrl, @UserAgent,@UserReferrer, @UserHostAddress, @UserHostName)", sconn))
           {
               scomm.Parameters.AddWithValue("@Country", country == null ? DBNull.Value : (object) country);
               scomm.Parameters.AddWithValue("@RawUrl", requestUrl == null ? DBNull.Value : (object)requestUrl);
               scomm.Parameters.AddWithValue("@UserAgent", userAgent == null ? DBNull.Value : (object)userAgent);
               scomm.Parameters.AddWithValue("@UserReferrer", referrer == null ? DBNull.Value : (object)referrer);
               scomm.Parameters.AddWithValue("@UserHostAddress", userIP == null ? DBNull.Value : (object)userIP);
               scomm.Parameters.AddWithValue("@UserHostName", string.IsNullOrEmpty(userHostName) ? DBNull.Value : (object)userHostName);
               scomm.ExecuteNonQuery();
           }
        }

        }

        public void EndLog(IAsyncResult res)
        {
            try
            {
                ((LogRequestDelegate)res.AsyncState).EndInvoke(res);
            }
            catch (Exception) { }
        }

    }
}

 

After MUCH better...this is pretty much identical to what I see in my IDE...yup, I use Verdana as a coding font...:

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using System.IO;

using System.Data;

using System.Data.Sql;

using System.Configuration;

using System.Data.SqlClient;

 

namespace ML

{

    class TrackingModule : IHttpModule

    {

        static string connectionString = ConfigurationManager.ConnectionStrings["subtextData"].ConnectionString;

 

 

        static IPCountryTable table;

        public void Dispose()

        {

 

        }

 

        public void Init(HttpApplication context)

        {

            context.BeginRequest += new EventHandler(context_BeginRequest);

 

        }

 

        void context_BeginRequest(object sender, EventArgs e)

        {

            HttpContext context = System.Web.HttpContext.Current;

 

            LogRequestDelegate ld = new LogRequestDelegate(LogRequest);

            AsyncCallback cb = new AsyncCallback(EndLog);

            ld.BeginInvoke(context, cb, ld);

 

        }

 

        public delegate void LogRequestDelegate(HttpContext context);

 

        public void LogRequest(HttpContext context)

        {

            if (context == null)

                return;

            if (!context.Request.Path.Contains(".aspx"))

                return;

 

            if (table == null)

            {

                string resourcePath = context.Server.MapPath(@"~\resources\");

                table = new IPCountryTable(16);

                table.LoadStatisticsFile(Path.Combine(resourcePath, "ripencc.latest"), true);

                table.LoadStatisticsFile(Path.Combine(resourcePath, "arin.latest"), true);

                table.LoadStatisticsFile(Path.Combine(resourcePath, "apnic.latest"), true);

                table.LoadStatisticsFile(Path.Combine(resourcePath, "lacnic.latest"), true);

            }

 

 

            string country = !string.IsNullOrEmpty(context.Request.UserHostAddress) ?  table.GetCountry(context.Request.UserHostAddress) : string.Empty;

            string requestUrl = context.Request.RawUrl;

            string userAgent = context.Request.UserAgent;

            string referrer = context.Request.UrlReferrer == null ? null : context.Request.UrlReferrer.ToString() ;

            string userIP = context.Request.UserHostAddress;

            string userHostName = System.Net.Dns.GetHostEntry(userIP).HostName;

            using(SqlConnection sconn = new SqlConnection(connectionString))

        {

            sconn.Open();

            using (SqlCommand scomm = new SqlCommand("INSERT INTO mostlylucid_Tracking (Country, RawUrl, UserAgent, UserReferrer, UserHostAddress, UserHostName) VALUES (@Country, @RawUrl, @UserAgent,@UserReferrer, @UserHostAddress, @UserHostName)", sconn))

           {

               scomm.Parameters.AddWithValue("@Country", country == null ? DBNull.Value : (object) country);

               scomm.Parameters.AddWithValue("@RawUrl", requestUrl == null ? DBNull.Value : (object)requestUrl);

               scomm.Parameters.AddWithValue("@UserAgent", userAgent == null ? DBNull.Value : (object)userAgent);

               scomm.Parameters.AddWithValue("@UserReferrer", referrer == null ? DBNull.Value : (object)referrer);

               scomm.Parameters.AddWithValue("@UserHostAddress", userIP == null ? DBNull.Value : (object)userIP);

               scomm.Parameters.AddWithValue("@UserHostName", string.IsNullOrEmpty(userHostName) ? DBNull.Value : (object)userHostName);

               scomm.ExecuteNonQuery();

           }

        }

 

        }

 

        public void EndLog(IAsyncResult res)

        {

            try

            {

                ((LogRequestDelegate)res.AsyncState).EndInvoke(res);

            }

            catch (Exception) { }

        }

 

    }

}