Even you do not use or analyze your application usage or user patterns for know you may need them in the feature. Even today you will be very surprised when you see tracking results. For example; Do you know how often google bots comes to your site? Which user use which feature or pages?
We must track and analyze our applications and users . At least we need to collect them now.
I will present very very simple solution. You only need add binary to your bin folder and add 2 lines to web.config.
If you need too track application specific information just put them in the httpContext that’s all.
Ok let's begin.
That's all. What you will get is like this
Every request logged per line, name value pairs delimited with tab character and name and value delimited with ‘|’ pipe character. To avoid collision default names prefixed wit “_” underscore.
Default tracking parameters are limited because I did not want to depend anything.
In your application if you want to track any information.
just put information into HttpContext’s items in as blow. Prefix names with “track:” this is vital because library check if item name prefixed with “track:”
This is just a sample
HttpContext.Current.Items["track:controller"] = controllerName;
1 using System;
2 using System.Collections;
3 using System.Diagnostics;
4 using System.Web;
5 using System.Text;
6
7
8 namespace Aksu.Tracking
9 {
10
11 public class TrackingModule : IHttpModule
12 {
13
14 public void Dispose()
15 {
16
17 }
18
19 public void Init(HttpApplication context)
20 {
21
22 context.BeginRequest += new EventHandler(context_BeginRequest);
23 context.EndRequest += new EventHandler(context_EndRequest);
24
25
26
27 var trackingFile = System.Configuration.ConfigurationManager.AppSettings["TrackingFile"];
28 if (string.IsNullOrEmpty(trackingFile))
29 {
30 trackingFile = @"c:\LogFiles\" + Guid.NewGuid().ToString() + "_{0}.log";
31 }
32 log = new Logger(trackingFile);
33 }
34
35 private static Logger log;
36
37
38
39 void context_EndRequest(object sender, EventArgs e)
40 {
41
42 var app = sender as HttpApplication;
43
44 if (app.Context.Items.Contains("__donttrack")) return;
45
46
47 if (app.User.Identity.IsAuthenticated)
48 {
49 app.Context.Items["track:_uid"] = app.User.Identity.Name;
50 }
51
52
53 var timer = app.Context.Items["__trackTimer"] as Stopwatch;
54 if (timer != null)
55 {
56 timer.Stop();
57 app.Context.Items["track:_pt"] = timer.ElapsedMilliseconds.ToString();
58 }
59
60 StringBuilder sb = new StringBuilder();
61
62 sb.AppendFormat("{0}|{1}\t", "_rd", DateTime.Now.ToString("s"));
63 sb.AppendFormat("{0}|{1}\t", "_rguid", Guid.NewGuid().ToString("N"));
64 foreach (DictionaryEntry entry in HttpContext.Current.Items)
65 {
66
67 var key = entry.Key as string;
68 if (key != null && key.StartsWith("track:"))
69 {
70 var realkey = key.Substring(6);
71 sb.AppendFormat("{0}|{1}\t", realkey, entry.Value);
72 }
73 }
74
75 log.WriteToLog(sb.ToString());
76
77 }
78
79
80 void context_BeginRequest(object sender, EventArgs e)
81 {
82 var app = sender as HttpApplication;
83
84 string filePath = app.Context.Request.FilePath;
85
86
87 // If request for static or image file do not track
88 // TODO: Take this extensions names from Application Settings
89
90
91 //if (filePath.EndsWith(".js")
92 // || filePath.EndsWith(".css")
93 // || filePath.EndsWith(".gif")
94 // || filePath.EndsWith(".png")
95 // )
96 //{
97 // app.Context.Items["__donttrack"] = true;
98 // return;
99 //}
100
101
102 Stopwatch timer = new Stopwatch();
103 timer.Start();
104 app.Context.Items["__trackTimer"] = timer;
105
106
107 string trackingId;
108 var trackingCookie = app.Request.Cookies["_tracking"];
109 if (trackingCookie == null)
110 {
111 trackingId = Guid.NewGuid().ToString("N");
112 var newCookie = new HttpCookie("_tracking");
113 newCookie.Value = trackingId;
114 newCookie.Expires = DateTime.Now.AddYears(10);
115 app.Response.Cookies.Add(newCookie);
116
117 }
118 else
119 {
120 trackingId = trackingCookie.Value;
121 }
122
123 app.Context.Items["track:_tid"] = trackingId;
124 app.Context.Items["track:_ip"] = app.Request.UserHostAddress;
125 app.Context.Items["track:_url"] = app.Request.Url.OriginalString;
126 app.Context.Items["track:_mtd"] = app.Request.RequestType;
127 app.Context.Items["track:_ref"] = app.Request.UrlReferrer == null ? "" : app.Request.UrlReferrer.AbsoluteUri;
128
129 }
130
131
132 }
133
134 }