前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用MongoDB存储访问者信息

使用MongoDB存储访问者信息

作者头像
张善友
发布2018-01-19 16:21:12
7800
发布2018-01-19 16:21:12
举报
文章被收录于专栏:张善友的专栏张善友的专栏

网站的访问者信息的存储一般都是海量的,通常使用关系数据库,现在NoSQL运动火热,满足这样的需求使用NoSQL数据库会更好,网站访问者信息主要是两个功能:

1、记录下网站的访问者信息

2、查询访问者信息和做相关的数据分析

本文采用MongoDB来记录访问者的信息的示例:

在asp.net中记录访问者信息的方法可以通过一个HttpHandler,在页面上放一个1像素的图片来请求这个HttpHandler,把他放到MasterPage页面就可以了。

下面给出ashx的代码

代码语言:js
复制
   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<Stat> coll = (MongoCollection<Stat>)mongo.GetCollection<Stat>();
  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 APIJSON API调用外,还提供了实现这些API的source code和所有的IP数据库,也就是说你只要下载这份code和database你也可以架设一个和IPinfoDB一样的网站,一样能够提供API服务,不过国外做的IP数据库对国内来说肯定不是很全很准,不过先将就着用吧。

访问访问者信息的MongoDB的信息记录:

代码语言:js
复制
   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。

相关文章:

http://www.cnblogs.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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-07-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档