Back to "Little hit tracking module for .TEXT 0.95 blogs..."

This is a viewer only at the moment see the article on how this works.

To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk

This is a preview from the server running through my markdig pipeline

Imported mostlylucidcouk

Little hit tracking module for .TEXT 0.95 blogs...

Tuesday, 06 July 2004

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:

<httpModules> <add name="RequestLogger" type="ExtraControls.ViewTrackModule, ExtraControls"/> </httpModules>

logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.