网站的访问者信息的存储一般都是海量的,通常使用关系数据库,现在NoSQL运动火热,满足这样的需求使用NoSQL数据库会更好,网站访问者信息主要是两个功能:
1、记录下网站的访问者信息
2、查询访问者信息和做相关的数据分析
本文采用MongoDB来记录访问者的信息的示例:
在asp.net中记录访问者信息的方法可以通过一个HttpHandler,在页面上放一个1像素的图片来请求这个HttpHandler,把他放到MasterPage页面就可以了。
下面给出ashx的代码
1: public class a : IHttpHandler
2: {
3: public void ProcessRequest(HttpContext ctx)
4: {
5: HttpBrowserCapabilities bc = ctx.Request.Browser;
6: Stat stat = new Stat();
7: stat._id = Guid.NewGuid();
8: stat.Browser = bc.Browser;
9: stat.Type = bc.Type;
10: stat.Version = bc.Version;
11: stat.Platform = bc.Platform;
12: stat.UrlReferrer = ctx.Request.UrlReferrer.ToString();
13: stat.UserHostAddress = ctx.Request.UserHostAddress;
14: stat.HttpMethod = ctx.Request.HttpMethod;
15: stat.IsAuthenticated = ctx.Request.IsAuthenticated;
16: stat.LogDateTime = DateTime.Now.ToLocalTime();
17:
18: WebClient wc=new WebClient();
19: try
20: {
21: string s =
22: wc.DownloadString("http://ipinfodb.com/ip_query.php?ip=" + stat.UserHostAddress + "&output=xml");
23: XmlDocument doc = new XmlDocument();
24: doc.LoadXml(s);
25: stat.Country = doc.DocumentElement.SelectNodes("CountryCode")[0].InnerText;
26: stat.State = doc.DocumentElement.SelectNodes("RegionName")[0].InnerText;
27:
28: stat.City = doc.DocumentElement.SelectNodes("City")[0].InnerText;
29: stat.Latitude = doc.DocumentElement.SelectNodes("Latitude")[0].InnerText;
30: stat.Longitude = doc.DocumentElement.SelectNodes("Longitude")[0].InnerText;
31:
32: }
33: catch(Exception ex)
34: {
35: System.Diagnostics.Debug.WriteLine(ex.Message+ ex.StackTrace );
36: }
37: finally
38: {
39: wc.Dispose();
40: }
41: using (Mongo mongo = Mongo.Create(Helper.ConnectionString() ))
42: {
43: MongoCollection coll = (MongoCollection)mongo.GetCollection();
44: coll.Save(stat);
45: }
46:
47: string sFileName = String.Empty;
48: string sPath = ctx.Server.MapPath(".");
49: try
50: {
51: sFileName = ctx.Request["name"].ToString().Trim();
52: if (sFileName.Length < 5) { return; } // must be at least "1.gif" (5 chars)
53: // serve the image that was requested:
54: ctx.Response.WriteFile(sPath + @"" + sFileName);
55: }
56: catch (Exception e)
57: {
58:
59: ctx.Response.Write(e.Message);
60: }
61: }
62: public bool IsReusable { get { return true; } }
63: }
上面代码使用到了HttpBrowserCapabilities,这里可以得到客户端的浏览器信息。还有客户端ip的来源使用到了ipinfodb.com这个服务,IPinfoDB网站非常的慷慨,慷慨到让人惊讶的程度,除了提供给你XML API和JSON API调用外,还提供了实现这些API的source code和所有的IP数据库,也就是说你只要下载这份code和database你也可以架设一个和IPinfoDB一样的网站,一样能够提供API服务,不过国外做的IP数据库对国内来说肯定不是很全很准,不过先将就着用吧。
访问访问者信息的MongoDB的信息记录:
1: [Serializable]
2: public class Stat
3: {
4: [MongoIdentifier]
5: public Guid _id {get;set;}
6: public string Type {get;set;}
7: public string Browser {get;set;}
8: public string Version {get;set;}
9: public string Platform {get;set;}
10: public string UrlReferrer {get;set;}
11: public string UserHostAddress {get;set;}
12: public bool IsAuthenticated {get;set;}
13: public string HttpMethod {get;set;}
14: public DateTime LogDateTime { get; set; }
15: public string City { get; set; }
16: public string State { get; set;}
17: public string Country { get; set; }
18: public string Latitude { get; set; }
19: public string Longitude { get; set; }
20: }
然后利用MongoDB的C# NORm驱动记录到MongoDB。
本文来自云栖社区合作伙伴“doNET跨平台”,了解相关信息可以关注“opendotnet”微信公众号