Keeping track on your downloads

When you provide downloadable content on your blog, you might want to keep track on downloads. With BlogEngine, it is incredibly easy to do – all you need is subscribe to one of the events exposed by core framework and log download information to any medium you like. For example, you want to log it in the tab delimited flat file so it is easy to export to Excel and do whatever kind of reporting you want to do.

First thing, we need to set references to the BlogEngine.Core libraries – so that core functionality will be available for our component.

  1. using BlogEngine.Core;
  2. using BlogEngine.Core.Web.HttpHandlers;
  3. using BlogEngine.Core.Web.Controls;

Then, subscribe to the FileHandler.Serving – this one fired every time BlogEngin serving files to the client. Just pass it function that you want to run when event fired, in our case it is FileHandler_Serving but you can name it whatever you like.

FileHandler.Serving += new EventHandler(FileHandler\_Serving);

Serving event let us to get hold on name of the file been downloaded – through the sender object. That’s pretty much all we need. If you want, you can collect any useful information about client from the request object, for example Request.UserAgent will tell you about operating system, browser, resolution and alike, so let’s grab it.

string fileName = sender.ToString();
string userAgent = HttpContext.Current.Request.UserAgent;

Now all we need is to store this information in the file, with .NET 2.0 it is piece of cake:

string \_logFile = HttpContext.Current.Server.MapPath("~/app\_data/DownloadCounter.txt");
StreamWriter sw;
if (!File.Exists(\_logFile))
    sw = File.CreateText(\_logFile);
    sw = File.AppendText(\_logFile);

The “line” is a string you build concatenating pieces of information you want to collect, like:

StringBuilder sb = new StringBuilder(fileName);

That’s all! Now you can go to Excel, pick Data/Import, select file and you’ll get all download info nicely formatted so you can print out a report and hang it over your bed to impress your girlfriend on the next visit – or may be you’ll find better things to do with this stuff.Anyways, you can download BlogEngine extension that will count downloads from your blog here:

Yes, I’ll count you :)


This site is all about developing web applications with focus on designing and building open source blogging solutions. Technologies include ASP.NET Core, C#, Angular, JavaScript and more.