# Little hit tracking module for .TEXT 0.95 blogs...

<datetime class="hidden">2004-07-06T00:00</datetime>
<!-- category -- mostlylucidcouk, Imported -->

OK, terribly documented, but it works...this is just the source for a little HttpModule which tracks hits to a .TEXT blog - pretty simple but should give anyone who wants to do it a start. I'll post a more complete version at some point (mainly just want to test how this skin behaves with posted code ;-))

using System;

using System.Web;

using System.Data;

using System.Data.SqlClient;

using Dottext.Framework;

using System.Collections.Specialized;

using System.IO;

using System.Text;

using System.Xml.Serialization;

using System.Threading;

using System.Net;

using Microsoft.ApplicationBlocks.Data;

namespace ExtraControls

{

/// 

/// Summary description for ViewTrackModule.

/// 

public class ViewTrackModule : IHttpModule

{

#region IHttpModule Members

public void Init(HttpApplication context)

{

context.EndRequest += new EventHandler(context\_EndRequest);

}

public void Dispose()

{

// TODO:  Add ViewTrackModule.Dispose implementation

}

#endregion

/// 

/// Ok, bit unusual...but I need the BlogConfig object to be available for the DB access...

/// 

/// 

/// 

private void context\_EndRequest(object sender, EventArgs e)

{

HttpApplication httpApp = sender as HttpApplication;

System.Uri  ur = httpApp.Request.UrlReferrer;

string testHost = string.Empty;

string thisHost = httpApp.Request.ServerVariables["SERVER\_NAME"];

if(ur != null)

{

testHost = ur.Host;

}

if(testHost != thisHost)

{

HttpRequest re = httpApp.Request;

LogToDBDel del = new LogToDBDel(LogToDB);

IAsyncResult ar = del.BeginInvoke(re,null,null);

}

}

private delegate void LogToDBDel(HttpRequest re);

private void LogToDB(HttpRequest re)

{

string m\_connectionString =  Dottext.Framework.Configuration.BlogConfigurationSettings.Instance().BlogProviders.DbProvider.ConnectionString;

string clientIP = re.UserHostAddress;

if(!clientIP.StartsWith("192.") && !clientIP.StartsWith("10."))

{

string clientUserAgent = re.UserAgent;

string clientReferer = re.ServerVariables["HTTP\_REFERER"];

string clientHostName = string.Empty;

try

{

clientHostName = Dns.GetHostByAddress(clientIP).HostName;

}

catch(Exception)

{

clientHostName = re.UserHostName;

}

string clientQueryString = re.RawUrl;

SqlParameter[] sqlParams = SqlHelperParameterCache.GetSpParameterSet(m\_connectionString, "pr\_Blog\_SaveEntryStats");        

sqlParams[0].Value = clientIP;

sqlParams[1].Value = clientUserAgent;

sqlParams[2].Value = clientHostName;

sqlParams[3].Value = clientQueryString;

sqlParams[4].Value = DateTime.Now;

sqlParams[5].Value = clientReferer;

try

{

SqlHelper.ExecuteNonQuery(m\_connectionString, CommandType.StoredProcedure, "pr\_Blog\_SaveEntryStats", sqlParams) ;

}

catch(Exception)

{

}

}

}

}

}

As usual, this needs an entry in the web.config to hook in the module:

&lt;httpModules&gt;
&lt;add name="RequestLogger" type="ExtraControls.ViewTrackModule, ExtraControls"/&gt;
&lt;/httpModules&gt;