zoukankan      html  css  js  c++  java
  • HTTPFilter 的例子

    using System;
    using System.Web;
    using System.Diagnostics;


    namespace HDI
    {
      public class HTTPFilter : IHttpModule
      {

        System.IO.StreamWriter fileWriter;
        HttpApplication applicationContext;
        EventHandler beginRequestEventHandler;
        String fileName;

        public void Init(HttpApplication context)
        {
          //Store file name for later use.
          fileName = context.Context.Request.PhysicalApplicationPath + "HTTPFilter.txt";

          //Open file and write information.
          fileWriter = new System.IO.StreamWriter(fileName, true);
          fileWriter.WriteLine(System.DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss:ffffff"));
          fileWriter.WriteLine("Init");
          fileWriter.Flush();
          fileWriter.Close();

          context.Error += (new EventHandler(this.Application_err));

                 


          //Store for access in other events.
          applicationContext = context;
          beginRequestEventHandler = new System.EventHandler(this.OnBeginRequest);
          applicationContext.BeginRequest += beginRequestEventHandler;
        }

        public void Dispose()
        {

          //Open file and write information.
          fileWriter = new System.IO.StreamWriter(fileName, true);
          fileWriter.WriteLine(System.DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss:ffffff"));
          fileWriter.WriteLine("Dispose");
          fileWriter.Flush();
          fileWriter.Close();

          //Clean-up.
          applicationContext.BeginRequest -= beginRequestEventHandler;
          beginRequestEventHandler = null;
        }

          protected void OnBeginRequest(object sender, EventArgs e)
          {
              //Open file and write information.
              fileWriter = new System.IO.StreamWriter(fileName, true);
              fileWriter.WriteLine(System.DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss:ffffff"));
              fileWriter.WriteLine("OnBeginRequest");
              fileWriter.WriteLine(applicationContext.Context.Request.UserAgent);
              fileWriter.WriteLine(applicationContext.Context.Request.UserHostAddress);
              fileWriter.WriteLine(applicationContext.Context.Request.Url.ToString());
              fileWriter.Flush();
              fileWriter.Close();
          }

          protected void Application_err(object sender, EventArgs e)
          {
              //Open file and write information.
              fileWriter = new System.IO.StreamWriter(fileName, true);
              fileWriter.WriteLine(System.DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss:ffffff"));
              fileWriter.WriteLine( applicationContext.Context.Error.Message+applicationContext.Context.AllErrors.ToString());

    //Context.Server.GetLastError()
              fileWriter.Flush();
              fileWriter.Close();
          }

      }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/starcrm/p/1344955.html
Copyright © 2011-2022 走看看