之前在《一種基于自定義代碼記錄使用者通路日志在Sharepoint網站的應用方法!》一文利用本人幾年前的開發的UserVisitLogsHelp元件進行了網站使用者通路日志記錄,可用于網站分析,雖然IIS可以有日志記錄的功能(也可通過工具分析),但它的主要缺點是可定制性相對較差,難以滿足個性化的需求,隻能用于windows平台的站點,有很大的局限性。通過該元件不僅能用于asp.net等windows平台網站,也可以用于PHP、JSP等Linux網站。該元件目前已在Codeplex網站中開源了,具體網址如下:http://uservisitlogshelp.codeplex.com/。
該元件實作原理很簡單:主要利用IHttpModule接口并在Web.config中的HttpModule節點添加此元件的配置,考慮到性能和可移植性,資料庫采用開源Sqlite,友善維護和資料采集。
Sqlite日志資料庫網站使用者通路日志表(UserVisitLog)結構設計如下:
編号 | 字段名稱 | 字段類型 | 備注 |
1 | Id | integer | 自增序号(PK,Not Null) |
2 | UserHostAddress | varchar(20) | 遠端用戶端的IP主機位址 |
3 | UserHostName | 遠端用戶端的DNS名稱 | |
4 | UrlAbsoluteUri | varchar(1600) | 目前請求的絕對URI |
5 | PhysicalPath | varchar(500) | 目前請求的URL相對應的實體檔案路徑 |
6 | UserAgent | varchar(1000) | 用戶端浏覽器的原始使用者代理資訊 |
7 | HttpMethod | varchar(4) | 用戶端使用的HTTP資料傳輸方法 |
8 | UserLanguages | 用戶端語言首選項的排序字元 | |
9 | UrlHost | varchar(100) | 用戶端主機的執行個體名 |
10 | UrlPort | varchar(10) | 目前URI的端口号 |
11 | TotalBytes | 目前輸入流中的位元組數 | |
12 | ContentLength | 用戶端發送的内容長度(以位元組計) | |
13 | IsLocal | varchar(5) | 目前請求是否來自本地計算機 |
14 | BrowserType | varchar(30) | 浏覽器的名稱和主(整數)版本号 |
15 | BrowserVersion | 浏覽器的完整版本号(包括整數和小數) | |
16 | BrowserPlatform | 用戶端使用的作業系統平台名稱 | |
17 | BrowserBeta | 浏覽器是否為測試版 | |
18 | BrowserActiveXControls | 浏覽器是否支援ActiveX控件 | |
19 | BrowserCookies | 浏覽器是否支援Cookie | |
20 | BrowserCrawler | 浏覽器是否為Web爬行周遊搜尋引擎 | |
21 | BrowserJavaScript | 浏覽器支援的EcmaScript主版本号 | |
22 | BrowserSupportsXmlHttp | 浏覽器是否支援通過HTTP接收XML | |
23 | BrowserInputType | 浏覽器支援的輸入類型 | |
24 | BrowserScreenPixelsWidth | 浏覽器顯示的近似寬度(機關像素) | |
25 | BrowserScreenPixelsHeight | 浏覽器顯示的近似高度(機關像素) | |
26 | UrlReferrerAbsoluteUri | 用戶端上次請求(該請求連結目前的URL)的絕對URI | |
27 | UrlReferrerAbsoluteUriDecode | 對UrlReferrerAbsoluteUri字段進行zh-cn或utf-9解碼 | |
28 | UrlReferrerHostName | 用戶端上次請求(該請求連結目前的URL)的DNS名稱 | |
29 | CanCombineFormsInDeck | 浏覽器是否支援包括多個視窗的卡片組 | |
30 | IsMobileDevice | 浏覽器是否為已識别的移動裝置 | |
31 | MobileDeviceManufacturer | 已知移動裝置制造商的名稱 | |
32 | MobileDeviceModel | 已知移動裝置的型号名 | |
33 | NumberOfSoftkeys | 移動裝置上軟鍵的數目 | |
34 | ContentEncoding | 内容字元的編碼 | |
35 | ScreenBitDepth | 浏覽器顯示的近似深度(機關像素) | |
36 | Website | 通路Web站點 | |
37 | WebCookies | varchar(80) | 記錄目前訪客的惟一Cookies值 |
38 | VisitTime | 目前請求通路時間 |
三種典型的應用場景:
1.asp.net(SharePoint) 網站
在web.config 的 <httpModules> 節點加入:
<httpModules>
<add name="WebsiteVisit" type="NetOpen_System.Component.WebsiteVisitHttpModule, NetOpen_System.Component.WebsiteVisit"/>
</httpModules>
2.SharePoint 網站
獨立部署的網站日志通路站點,可以通過JS代碼方式進行頁面跟蹤,對于企業内部網站可以取得登入AD的使用者資訊:
<script type="text/javascript" >
SP.SOD.executeOrDelayUntilScriptLoaded(runMyCode, "SP.js");
var currentUser = null;
var currentUserTitle=null;
function runMyCode() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
var user = web.get_currentUser();
user.retrieve();
ctx.executeQueryAsync(
function () {
//only in the success case you can work with user login
currentUser= user.get_loginName();
currentUserTitle = user.get_title();
document.getElementById("randimg").src="http://webloggersite/Default.aspx?UserName=" +currentUser+"&UserTitle="+currentUserTitle;
},
function (data) {
//notify the failure
});
}
</script>
<img id="randimg" name="randimg" src="" style="width:0;height:0;" />
3.其他網站(PHP,JSP,ASP等)
網站頁面JS跟蹤代碼:
<script type="text/javascript">
function addImg(isrc) {
var Img = new Image();
Img.style = "width:0;height:0;";
Img.onload = function () {
document.body.appendChild(Img);
}
Img.src = isrc;
}
addImg("http://webloggersite/Default.aspx");
自動生成日志資料庫Sqlite的配置檔案(日志資料庫可按年,按月,按天或不生成等):
NetOpen_SystemWebsiteVisit.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<NetOpen_System>
<WebsiteVisit>
<!--DateSource Automatic Generation of Database File year:one year month:every month day:every day None:Does not generate -->
<SQLiteConnectings DataSource="~/Visit_Data/HomeWeb" Password="12345678" DateSource="day" Website="" ExcludeUrl="" DecodeUrl="" TextDecoding="utf-8" ExcludeUserAgent="" WebCookiesName="" WebCookiesExpires="3650"/>
</WebsiteVisit>
</NetOpen_System>
本部落格為軟體人生原創,歡迎轉載,轉載請标明出處:http://www.cnblogs.com/nbpowerboy/p/3158507.html。演繹或用于商業目的,但是必須保留本文的署名軟體人生(包含連結)。如您有任何疑問或者授權方面的協商,請給我留言。SharePoint商業智能技術QQ群:140668362,.Net技術交流QQ群:195516928,歡迎各位加入交流。 |