对WEB Request监控

本文介绍了一个用于Web请求监控及日志记录的实用工具类WebMonitor。该类能够根据预设路径监测HTTP请求,并将请求信息以XML格式保存到指定的日志文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值