您的位置:首页 > 数据库 > Mongodb

使用MongoDB存储访问者信息

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

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

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

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

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

下面给出ashx的代码

[code] public class a : IHttpHandler


{


public void ProcessRequest(HttpContext ctx)


   {


HttpBrowserCapabilities bc = ctx.Request.Browser;


Stat stat = new Stat();


stat._id = Guid.NewGuid();


stat.Browser = bc.Browser;


stat.Type = bc.Type;


stat.Version = bc.Version;


stat.Platform = bc.Platform;


stat.UrlReferrer = ctx.Request.UrlReferrer.ToString();


stat.UserHostAddress = ctx.Request.UserHostAddress;


stat.HttpMethod = ctx.Request.HttpMethod;


stat.IsAuthenticated = ctx.Request.IsAuthenticated;


stat.LogDateTime = DateTime.Now.ToLocalTime();


 


WebClient wc=new WebClient();


try


  {


string s =


wc.DownloadString("http://ipinfodb.com/ip_query.php?ip=" + stat.UserHostAddress + "&output=xml");


XmlDocument doc = new XmlDocument();


doc.LoadXml(s);


stat.Country = doc.DocumentElement.SelectNodes("CountryCode")[0].InnerText;


stat.State = doc.DocumentElement.SelectNodes("RegionName")[0].InnerText;


 


stat.City = doc.DocumentElement.SelectNodes("City")[0].InnerText;


stat.Latitude = doc.DocumentElement.SelectNodes("Latitude")[0].InnerText;


stat.Longitude = doc.DocumentElement.SelectNodes("Longitude")[0].InnerText;




}


catch(Exception ex)


  {


System.Diagnostics.Debug.WriteLine(ex.Message+ ex.StackTrace );


}


finally


  {


wc.Dispose();


}


using (Mongo mongo = Mongo.Create(Helper.ConnectionString()  ))


  {


MongoCollection<Stat> coll = (MongoCollection<Stat>)mongo.GetCollection<Stat>();


coll.Save(stat);


}




 string sFileName = String.Empty;


 string sPath = ctx.Server.MapPath(".");


try


  {


sFileName = ctx.Request["name"].ToString().Trim();


if (sFileName.Length < 5){ return;}  // must be at least "1.gif" (5 chars)


// serve the image that was requested:


ctx.Response.WriteFile(sPath + @"\" +  sFileName);


}


 catch (Exception e)


  {


 


ctx.Response.Write(e.Message);


}


}


     public bool IsReusable{ get{ return true;}}


}

[/code]

上面代码使用到了HttpBrowserCapabilities,这里可以得到客户端的浏览器信息。还有客户端ip的来源使用到了ipinfodb.com这个服务,IPinfoDB网站非常的慷慨,慷慨到让人惊讶的程度,除了提供给你XML APIJSON API调用外,还提供了实现这些API的source code和所有的IP数据库,也就是说你只要下载这份code和database你也可以架设一个和IPinfoDB一样的网站,一样能够提供API服务,不过国外做的IP数据库对国内来说肯定不是很全很准,不过先将就着用吧。

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

[code] [Serializable]


public class Stat


{


[MongoIdentifier]


 public Guid _id{get;set;}


 public string Type{get;set;}


 public string Browser{get;set;}


 public string Version{get;set;}


 public string Platform{get;set;}


 public string UrlReferrer{get;set;}     


 public string UserHostAddress{get;set;}


 public bool IsAuthenticated{get;set;}


 public string HttpMethod{get;set;}


 public DateTime LogDateTime{ get; set;}


 public string City{ get; set;}


 public string State{ get; set;}


 public string Country{ get; set;}


 public string Latitude{ get; set;}


 public string Longitude{ get; set;}


}

[/code]

然后利用MongoDB的C# NORm驱动记录到MongoDB。

相关文章:

/article/4588113.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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: