62,050
社区成员
发帖
与我相关
我的任务
分享
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
//统计初始化
Count_Start();
}
void Application_End(object sender, EventArgs e)
{
Count_SaveCache();
}
void Application_Error(object sender, EventArgs e)
{
//记录出错的请求
Exception LastError = Server.GetLastError();
Count_RecordRequest(LastError.Message);
}
void Application_PostRequestHandlerExecute(object sender, EventArgs e)
{
//记录未出错的请求
Count_RecordRequest(Response.StatusDescription);
}
void Session_Start(object sender, EventArgs e)
{
//记录新的Session
Count_RecordSession();
}
#region 统计的全部实现 Count_Start() Count_RecordSession() Count_RecordRequest(string StatusDescription) Count_Save(string sql) Count_SaveCache()
string ReplaceSingleQuotes(string str)
{
if(string.IsNullOrEmpty(str))
return string.Empty;
else
return str.Replace("'", "''");
}
void Count_Start()
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath(@"~/Count.config"));
string ConnStr = xdoc.DocumentElement.SelectSingleNode(@"/Settings/ConnStr").InnerText;
List<String> IngoreList = new List<string>();
foreach(XmlNode node in xdoc.DocumentElement.SelectNodes(@"/Settings/Ingore"))
IngoreList.Add(node.InnerText);
Application["Count_ConnStr"] = ConnStr;
Application["Count_IngoreList"] = IngoreList;
Application["Count_Cache"] = new List<string>();
}
void Count_RecordSession()
{
List<string> Count_Cache = (List<string>)Application["Count_Cache"];
Session["Count_gID"] = Guid.NewGuid().ToString();
string sql = string.Format("INSERT INTO [Count_Session] ([DateTime],[gID],[REMOTE_HOST],[HTTP_ACCEPT_LANGUAGE],[HTTP_USER_AGENT],[HTTP_UA_CPU]) values ('{0}','{1}','{2}','{3}','{4}','{5}')",
DateTime.Now, Session["Count_gID"], Request.ServerVariables["REMOTE_HOST"], ReplaceSingleQuotes(Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]), ReplaceSingleQuotes(Request.ServerVariables["HTTP_USER_AGENT"]), ReplaceSingleQuotes(Request.ServerVariables["HTTP_UA_CPU"]));
Count_Save(sql);
}
void Count_RecordRequest(string StatusDescription)
{
List<string> Count_Cache = (List<string>)Application["Count_Cache"];
List<string> IngoreList = (List<string>)Application["Count_IngoreList"];
string url = Request.RawUrl;
//ingore some urls
foreach(string str in IngoreList)
if(url.ToLower().IndexOf(str.ToLower()) != -1)
return;
string sql = string.Format("INSERT INTO [Count_Request] ([DateTime],[gID],[RawUrl],[HTTP_REFERER],[QUERY_STRING],[REQUEST_METHOD],[IsSecureConnection],[StatusDescription]) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",
DateTime.Now, (HttpContext.Current.Session == null) ? null : Session["Count_gID"], ReplaceSingleQuotes(url), ReplaceSingleQuotes(Request.ServerVariables["HTTP_REFERER"]), ReplaceSingleQuotes(Request.ServerVariables["QUERY_STRING"]), Request.ServerVariables["REQUEST_METHOD"], Request.IsSecureConnection ? 1 : 0, ReplaceSingleQuotes(StatusDescription));
Count_Save(sql);
}
void Count_Save(string sql)
{
List<string> Count_Cache = (List<string>)Application["Count_Cache"];
Application.Lock();
Count_Cache.Add(sql);
if(Count_Cache.Count >= 5)
Count_SaveCache();
Application.UnLock();
}
void Count_SaveCache()
{
try
{
OleDbConnection Count_OleConn = new OleDbConnection((string)Application["Count_ConnStr"]);
Count_OleConn.Open();
List<string> Count_Cache = (List<string>)Application["Count_Cache"];
foreach(string sql in Count_Cache)
{
try
{
OleDbCommand Count_OleCmd = new OleDbCommand(sql, Count_OleConn);
Count_OleCmd.ExecuteNonQuery();
}
catch(Exception ee)
{
Count_SaveException(ee);
}
}
Count_Cache.Clear();
Count_OleConn.Close();
}
catch(Exception ee)
{
Count_SaveException(ee);
}
}
void Count_SaveException(Exception ee)
{
StreamWriter sw = new StreamWriter(Server.MapPath("~/Count_errors.txt"), true);
sw.WriteLine(DateTime.Now);
sw.WriteLine("Message:{0}\n\rStackTrace:{1}", ee.Message, ee.StackTrace);
sw.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
sw.Flush();
sw.Close();
}
#endregion
</script>
void Count_Save(string sql)
{
List<string> Count_Cache = (List<string>)Application["Count_Cache"];
Application.Lock();
Count_Cache.Add(sql);
//在服务器上
//这里修改成10就怎么都不行
//查找不到任何错误记录
//每次记录数超过10就自动清空了Count_Cache
//在本地修改成10就不会有任何问题。
if(Count_Cache.Count >= 5)
Count_SaveCache();
Application.UnLock();
}
using System;
using System.Collections.Generic;
public partial class Count_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Session.Timeout = 100;
Response.Write(Session.Timeout);
Response.Write("<br>");
List<string> list = ASP.global_asax.Count_Test;
list.Add(Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString());
Response.Write(list.Count.ToString() + "<br>");
foreach(string item in list)
{
Response.Write(item + "<br>");
}
GC.Collect();
}
}
static List<int> a=new List<int>;
void todo(){
lock(a)
a.Add(10);
}