當用戶點擊一個鏈接的時候,記錄下用戶在這個session用點擊了這個資源:
在鏈接中觸發onlick函數, onlick=ClickLog(clickLogText), 這是一個用ajax處理的異步請求 。
js文件中的代碼:
function ClickLog(clickLogText) {
//alert("fefffffffffffe");
var xmlHttp;
//alert("fefffffffffffe");
var xmlHttp;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("您的瀏覽器不支持AJAX!");
return false;
}
}
}
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("您的瀏覽器不支持AJAX!");
return false;
}
}
}
xmlHttp.open("GET", "/WriteLog.aspx?text=" + clickLogText, true);
xmlHttp.send(null);
// xmlHttp.onreadystatechange = function() {
// if (xmlHttp.readyState == 4) {
// 不需要界面響應,
xmlHttp.send(null);
// xmlHttp.onreadystatechange = function() {
// if (xmlHttp.readyState == 4) {
// 不需要界面響應,
// }
// }
// }
其中WriteLog.aspx.cs中的代碼是:這裏要注意一個多線程的問題,因爲多個用戶可能同時需要操作一個文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.IO;
namespace Gucas
{
public partial class WriteLog : System.Web.UI.Page
{
static Mutex m_WriteMutex = new Mutex();
{
public partial class WriteLog : System.Web.UI.Page
{
static Mutex m_WriteMutex = new Mutex();
protected void Page_Load(object sender, EventArgs e)
{
string text = Request.QueryString["text"];
string fileName = @".\Log\ClickLog" + DateTime.Now.ToString(".yyyMMdd");
writeLine(fileName,text);
}
{
string text = Request.QueryString["text"];
string fileName = @".\Log\ClickLog" + DateTime.Now.ToString(".yyyMMdd");
writeLine(fileName,text);
}
public void writeLine(string fileName, string dataText)
{
FileStream fs = null;
StreamWriter sw = null;
//用於處理多線程
m_WriteMutex.WaitOne();
{
FileStream fs = null;
StreamWriter sw = null;
//用於處理多線程
m_WriteMutex.WaitOne();
try
{
//CHECK文件存在不
if (!File.Exists(fileName))
{
FileStream tempfs = File.Create(fileName);
tempfs.Close();
}
{
//CHECK文件存在不
if (!File.Exists(fileName))
{
FileStream tempfs = File.Create(fileName);
tempfs.Close();
}
fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None);
fs.Seek(0, System.IO.SeekOrigin.End);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(dataText);
fs.Seek(0, System.IO.SeekOrigin.End);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(dataText);
if (sw != null)
{
sw.Close();
sw = null;
}
if (fs != null)
{
fs.Close();
fs = null;
}
{
sw.Close();
sw = null;
}
if (fs != null)
{
fs.Close();
fs = null;
}
}
catch (Exception ex)
{
}
{
}
finally
{
try
{
if (sw != null)
{
sw.Close();
sw = null;
}
{
try
{
if (sw != null)
{
sw.Close();
sw = null;
}
if (fs != null)
{
fs.Close();
fs = null;
}
}
catch
{
}
{
fs.Close();
fs = null;
}
}
catch
{
}
m_WriteMutex.ReleaseMutex();
}
}
}
}
}