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
  • 相关阅读:
    C#生成CHM文件(外篇使用hha.dll)
    prototype原理详解
    避免在代码中直接任意使用ConfigurationManager.AppSettings
    页面生命周期的来龙去脉(详细)
    nRF52832 SAADC sampling
    nRF52832开发日志--SAADC调试
    boost学习笔记(七)---date_time库
    Boost学习笔记(六) progress_display注意事项
    Boost学习笔记(五) progress_display
    扩展progress_timer的计时精度
  • 原文地址:https://www.cnblogs.com/starcrm/p/1344955.html
Copyright © 2011-2022 走看看