动态注册HttpModule管道,实现global.asax功能
1.所用类库有 Microsoft.Web.Infrastructure.dll 和WebActivator.dll
2.类代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using WebActivator;
/// <summary>
///Test 的摘要说明
/// </summary>
///
[assembly: WebActivator.PreApplicationStartMethod(typeof(RegisteModule.PreApplicationStartCode), "PreStart")]
namespace RegisteModule
{
public class CustomModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.Error += new EventHandler(application_Error); } void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication ap = sender as HttpApplication;
if (ap != null)
{
string msg = " context_BeginRequest=="+DateTime.Now+"\n\r";
System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("~/log.txt"),msg);
ap.Response.Write("测试PreApplicationStartMethod<br/>"); } } void context_ErrorRequest(object sender, EventArgs e)
{
HttpApplication ha = sender as HttpApplication;
var error = ha.Server.GetLastError();
var code = (error is HttpException) ? (error as HttpException).GetHttpCode() : ;
if (code != )
{
} //记录到日志文件 System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("~/log.txt"), error.InnerException.ToString()+"\n\r"); ha.Server.ClearError();
ha.Response.Write(error.Message);
ha.Response.End();
} void application_Error(object sender, EventArgs e)
{ HttpApplication ha = sender as HttpApplication;
var Server = ha.Server;
var Request = ha.Request;
// 在出现未处理的错误时运行的代码
Exception ex = Server.GetLastError().GetBaseException();
StringBuilder str = new StringBuilder();
str.Append("\r\n" + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"));
str.Append("\r\n.客户信息:"); string ip = "";
if (Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR") != null)
{
ip = Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR").ToString().Trim();
}
else
{
ip = Request.ServerVariables.Get("Remote_Addr").ToString().Trim();
}
str.Append("\r\n\tIp:" + ip);
str.Append("\r\n\t浏览器:" + Request.Browser.Browser.ToString());
str.Append("\r\n\t浏览器版本:" + Request.Browser.MajorVersion.ToString());
str.Append("\r\n\t操作系统:" + Request.Browser.Platform.ToString());
str.Append("\r\n.错误信息:");
str.Append("\r\n\t页面:" + Request.Url.ToString());
str.Append("\r\n\t错误信息:" + ex.Message);
str.Append("\r\n\t错误源:" + ex.Source);
str.Append("\r\n\t异常方法:" + ex.TargetSite);
str.Append("\r\n\t堆栈信息:" + ex.StackTrace);
str.Append("\r\n--------------------------------------------------------------------------------------------------");
//创建路径
string upLoadPath = Server.MapPath("~/log/");
if (!System.IO.Directory.Exists(upLoadPath))
{
System.IO.Directory.CreateDirectory(upLoadPath);
}
//创建文件 写入错误
System.IO.File.AppendAllText(upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + ".log", str.ToString(), System.Text.Encoding.UTF8);
//处理完及时清理异常
Server.ClearError();
} public void Dispose()
{
//nothing to do here
}
} public class PreApplicationStartCode
{
private static bool hasLoaded; public static void PreStart()
{
if (!hasLoaded)
{
hasLoaded = true;
//注意这里的动态注册,此静态方法在Microsoft.Web.Infrastructure.DynamicModuleHelper
DynamicModuleUtility.RegisterModule(typeof(CustomModule));
}
}
}
}
3.测试代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ throw new Exception("this is error");
}
}
参考内容 http://www.cnblogs.com/TomXu/p/3756846.html
动态注册HttpModule管道,实现global.asax功能的更多相关文章
- Mvc动态注册HttpModule详解
序言 注册Httpmodule可以让我们使用HttpApplication对象中的处理管道事件.目前大家所熟知的应该有2种方式来使用HttpApplication对象中的处理管道事件.第一种是通过Gl ...
- 你必须知道ASP.NET知识------关于动态注册httpmodule(对不起汤姆大叔)
一.关于动态注册的问题 很多人看过汤姆大叔的MVC之前的那点事儿系列(6):动态注册HttpModule ,其实汤姆大叔没有发现httpmodule动态注册的根本机制在哪里. 亦即:怎么动态注册?为什 ...
- 不使用配置文件动态注册HttpModule
在asp.net 4.0中,提供了一种不通过修改配置文件注册Module的方法.从.net3.5开始,新提供的PreApplicationStartMethodAttribute特性可以应用在程序集上 ...
- MVC之前的那点事儿系列(6):动态注册HttpModule
文章内容 通过前面的章节,我们知道HttpApplication在初始化的时候会初始化所有配置文件里注册的HttpModules,那么有一个疑问,能否初始化之前动态加载HttpModule,而不是只从 ...
- 动态注册HttpModule
动态注册HttpModule 2014-06-05 08:58 by 汤姆大叔, 757 阅读, 4 评论, 收藏, 编辑 文章内容 通过前面的章节,我们知道HttpApplication在初始化的时 ...
- MVC源码解析 - 配置注册 / 动态注册 HttpModule
本来这一篇, 是要继续 Pipeline 的, 但是在 Pipeline之前, 我看到了InitModules()方法, 所以决定, 在中间穿插一篇进来. 这一篇来讲一下 IHttpModule 的加 ...
- 在Asp.net 4.0 中动态注册HttpModule
using System; using System.Web; using Microsoft.Web.Infrastructure; namespace MvcApplication1 { publ ...
- ASP.NET MVC中注册Global.asax的Application_Error事件处理全局异常
在ASP.NET MVC中,通过应用程序生命周期中的Application_Error事件可以捕获到网站引发的所有未处理异常.本文作为学习笔记,记录了使用Global.asax文件的Applicati ...
- MEF在WCF REST中实际应用2(Global.asax注册)
IOCContainer文件: public class IOCContainer { /// <summary> /// 容器 /// </summary> public s ...
随机推荐
- 51 nod 1419 最小公倍数挑战【数论/互质+思维】
1419 最小公倍数挑战 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 几天以前,我学习了最小公倍数.玩得挺久了 ...
- Frequency
题目描述 Snuke loves constructing integer sequences. There are N piles of stones, numbered 1 through N. ...
- 最基础的java
一.Window中常见的dos命令 在哪里操作dos命令: Win7 ---> 开始 ---->所有程序--->附件---->命令提示符 Win7--> 开始 -- ...
- Ugly Number II -- LeetCode
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Moving Average from Data Stream -- LeetCode
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- 洛谷 U19159 采摘毒瘤
题目背景 Salamander见到路边有如此多的毒瘤,于是见猎心喜,从家里拿来了一个大袋子,准备将一些毒瘤带回家. 题目描述 路边共有nn 种不同的毒瘤,第i 种毒瘤有k_i 个,每个需要占据d_i ...
- OpenResty域名could not be resolved及dnsmasq配置
在本地开发中使用自己配置的域名例如:wuyachao.com配置在/etc/hosts,ping wuyachao.com显示ip为127.0.0.1,在使用lua_resty_http时候,会报错 ...
- 八. 输入输出(IO)操作7.文件的随机读写
Java.io 包提供了 RandomAccessFile 类用于随机文件的创建和访问.使用这个类,可以跳转到文件的任意位置读写数据.程序可以在随机文件中插入数据,而不会破坏该文件的其他数据.此外,程 ...
- bin/...的访问被拒绝被拒绝的问题
复制到bin.... 对路径bin/.... 的访问被拒绝出现这们的问题,把源码管理器中项目的Bin目录删除,重新获取就要以了
- 程序编译时书写Makefile注意事项一例
在进行程序编译时,可能需要指定一些库的库的路径.头文件的路径,分别使用的参数选项是-L和-I,需要注意的是: 需要确保-L和-I后边的内容不为空,否则会出现意想不到的错误,而这种错误比较难以发现,引起 ...