public class WebMonitor
{
public static string[] MonitorPath = { "/aa" };
public static bool ThrowException { get; set; }
public static int LogBuffering = 10240;
public static string LogFile { get; set; }
private static System.Text.StringBuilder oBuilder;
public static void Monitor(System.Web.HttpRequest Request)
{
bool bNeedMonitor = false;
foreach (string m in MonitorPath)
{
if (Request.Path.StartsWith(m))
{
bNeedMonitor = true;
break;
}
}
if (!bNeedMonitor)
{
return;
}
System.Text.StringBuilder oB = new System.Text.StringBuilder();
oB.AppendLine("<url name=/"" + System.Web.HttpUtility.HtmlEncode(Request.FilePath) + "/" method=/"" + Request.RequestType + "/">");
foreach (string m in Request.QueryString.AllKeys)
{
oB.AppendLine("<item name=/"" + System.Web.HttpUtility.HtmlEncode(m) + "/" value=/"" + System.Web.HttpUtility.HtmlEncode(Request[m]) + "/" method=/"GET/" />");
}
foreach (string m in Request.Form.AllKeys)
{
oB.AppendLine("<item name=/"" + System.Web.HttpUtility.HtmlEncode(m) + "/" value=/"" + System.Web.HttpUtility.HtmlEncode(Request.Form[m]) + "/" method=/"POST/" />");
}
oB.AppendLine("</url>");
SaveLog(oB.ToString(),Request);
}
private static void SaveLog(string m,System.Web.HttpRequest Request)
{
if (oBuilder == null)
{
oBuilder = new System.Text.StringBuilder(LogBuffering + 1024);
}
lock (oBuilder)
{
oBuilder.Append(m);
if (oBuilder.Length > LogBuffering)
{
if (string.IsNullOrEmpty(LogFile))
{
System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo(Request.MapPath("~"));
LogFile = oDir.Parent.FullName + "//Request"+System.DateTime.Now.ToString("yyyymmdd")+".log";
if (!System.IO.Directory.Exists(oDir.Parent.FullName))
{
try
{
System.IO.Directory.CreateDirectory(oDir.Parent.FullName);
}
catch (System.Exception oError)
{
}
}
}
try
{
System.IO.File.AppendAllText(LogFile, oBuilder.ToString(), System.Text.Encoding.UTF8);
}
catch
{
}
oBuilder.Length = 0;
}
}
}
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
WebMonitor.Monitor(Request);
}