运用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")[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
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。

连带文章:

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

网站地图xml地图