沈阳格力专卖店 发表于 2015-7-6 06:04:01

使用MongoDB存储访问者信息

  网站的访问者信息的存储一般都是海量的,通常使用关系数据库,现在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").InnerText;

26:             stat.State = doc.DocumentElement.SelectNodes("RegionName").InnerText;

27:

28:             stat.City = doc.DocumentElement.SelectNodes("City").InnerText;

29:             stat.Latitude = doc.DocumentElement.SelectNodes("Latitude").InnerText;

30:             stat.Longitude = doc.DocumentElement.SelectNodes("Longitude").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 &quot;1.gif&quot; (5 chars)

53:             // serve the image that was requested:

54:             ctx.Response.WriteFile(sPath + @&quot;\&quot; +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:

   2: public class Stat

   3: {

   4:

   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。
  相关文章:
  http://www.iyunv.com/shanyou/archive/2010/06/04/1751734.html
  http://www.eggheadcafe.com/tutorials/aspnet/63de8012-127a-4478-8725-3e1c27969596/nosql-mongodb-install-lotus-notes-and-couchdb.aspx
http://www.eggheadcafe.com/tutorials/aspnet/51d3ae19-d6f9-4807-ac0a-0baab2964b03/mongodb-install-as-service-and-use-net-drivers.aspx
http://www.eggheadcafe.com/tutorials/aspnet/93206c89-09c9-40fc-9296-7d74bb7996ad/a-mongodb-cache-utility.aspx
  http://www.eggheadcafe.com/tutorials/aspnet/3a73c6de-82a1-4690-a7aa-d0eda58203f7/store-aspnet-site-visitor-stats-in-mongodb.aspx
  http://www.eggheadcafe.com/tutorials/aspnet/27f836b7-2c9e-4942-9712-1c7b901cadcc/aspnet-providerless-custom-forms-authentication-roles-and-profile-with-mongodb.aspx
页: [1]
查看完整版本: 使用MongoDB存储访问者信息