全局处理函数(处理session、异常等)
2011-01-12 23:22
218 查看
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Xml;
/// <summary>
///GlobalException 的摘要说明
/// </summary>
public class GlobalFun : System.Web.IHttpModule
{
public GlobalFun()
{
}
public String ModuleName
{
get { return "GlobalException"; }
}
public void Init(System.Web.HttpApplication context)
{
//全局访问函数
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
//全局错误函数
context.Error += new EventHandler(ShowError);
}
/// <summary>
/// 全局访问函数(当有访问对象时,先执行此方法)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void context_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication httpApplicationOne = (HttpApplication)sender;
string fileName = httpApplicationOne.Context.Request.FilePath.Substring(httpApplicationOne.Context.Request.FilePath.LastIndexOf("/") + 1, httpApplicationOne.Context.Request.FilePath.Length - httpApplicationOne.Context.Request.FilePath.LastIndexOf("/") - 1);
if (fileName.IndexOf("aspx") > 0)
{
if (!(fileName == "Code.aspx" || fileName == "error.aspx" || fileName == "login.aspx" || fileName == "LoginOut.aspx"))
{
if (httpApplicationOne.Context.Session["userPower"] == null)
{
//处理方法
//httpApplicationOne.Context.Response.Redirect("~/error.aspx?msg=session is go out");
}
}
}
}
/// <summary>
/// 全局错误函数(当有错误出现时,执行此方法)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ShowError(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
#region 写入xml日志文件
string filePath = context.Server.MapPath("./");
filePath += "errorLog.xml";
//先判断是否存在此文件
if (File.Exists(filePath))
{
writeXml(filePath, context);//若存在,则直接在后面加入新的节点
}
else
{
//若不存在,则先生成文件
createFile(filePath);
//最后在文件中加入新的节点
writeXml(filePath, context);
}
#endregion
}
#region 生成xml文件
private static void createFile(string filePath)
{
//若不存在,则先生成文件
FileStream myFs = new FileStream(filePath, FileMode.OpenOrCreate); //创建文件流
myFs.Close(); //关闭文件流
//生成文件后,先加入一个最大的节点log
XmlTextWriter xmlWrite = new XmlTextWriter(filePath, null);
xmlWrite.Formatting = Formatting.Indented; //格式化
xmlWrite.WriteComment("错误收集文档"); //写入注释
xmlWrite.WriteStartElement("log");
xmlWrite.WriteEndElement();
xmlWrite.Close();
}
#endregion
#region 写入xml
private static void writeXml(string filePath,HttpContext context)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode root = xmlDoc.SelectSingleNode("log"); //查找<log>
XmlElement xe1 = xmlDoc.CreateElement("error"); //创建一个<error>节点
xe1.SetAttribute("time", Convert.ToString(DateTime.Now)); //设置该节点time属性
XmlElement xesub1 = xmlDoc.CreateElement("url");
xesub1.InnerText = context.Request.Url.AbsoluteUri; //设置文本
xe1.AppendChild(xesub1); //添加到<error>节点中
XmlElement xesub2 = xmlDoc.CreateElement("msg");
xesub2.InnerText = context.Server.GetLastError().GetBaseException().Message;
xe1.AppendChild(xesub2);
root.AppendChild(xe1); //添加到<log>节点中
xmlDoc.Save(filePath);
}
#endregion
public void Dispose()
{
}
}
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Xml;
/// <summary>
///GlobalException 的摘要说明
/// </summary>
public class GlobalFun : System.Web.IHttpModule
{
public GlobalFun()
{
}
public String ModuleName
{
get { return "GlobalException"; }
}
public void Init(System.Web.HttpApplication context)
{
//全局访问函数
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
//全局错误函数
context.Error += new EventHandler(ShowError);
}
/// <summary>
/// 全局访问函数(当有访问对象时,先执行此方法)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void context_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication httpApplicationOne = (HttpApplication)sender;
string fileName = httpApplicationOne.Context.Request.FilePath.Substring(httpApplicationOne.Context.Request.FilePath.LastIndexOf("/") + 1, httpApplicationOne.Context.Request.FilePath.Length - httpApplicationOne.Context.Request.FilePath.LastIndexOf("/") - 1);
if (fileName.IndexOf("aspx") > 0)
{
if (!(fileName == "Code.aspx" || fileName == "error.aspx" || fileName == "login.aspx" || fileName == "LoginOut.aspx"))
{
if (httpApplicationOne.Context.Session["userPower"] == null)
{
//处理方法
//httpApplicationOne.Context.Response.Redirect("~/error.aspx?msg=session is go out");
}
}
}
}
/// <summary>
/// 全局错误函数(当有错误出现时,执行此方法)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ShowError(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
#region 写入xml日志文件
string filePath = context.Server.MapPath("./");
filePath += "errorLog.xml";
//先判断是否存在此文件
if (File.Exists(filePath))
{
writeXml(filePath, context);//若存在,则直接在后面加入新的节点
}
else
{
//若不存在,则先生成文件
createFile(filePath);
//最后在文件中加入新的节点
writeXml(filePath, context);
}
#endregion
}
#region 生成xml文件
private static void createFile(string filePath)
{
//若不存在,则先生成文件
FileStream myFs = new FileStream(filePath, FileMode.OpenOrCreate); //创建文件流
myFs.Close(); //关闭文件流
//生成文件后,先加入一个最大的节点log
XmlTextWriter xmlWrite = new XmlTextWriter(filePath, null);
xmlWrite.Formatting = Formatting.Indented; //格式化
xmlWrite.WriteComment("错误收集文档"); //写入注释
xmlWrite.WriteStartElement("log");
xmlWrite.WriteEndElement();
xmlWrite.Close();
}
#endregion
#region 写入xml
private static void writeXml(string filePath,HttpContext context)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode root = xmlDoc.SelectSingleNode("log"); //查找<log>
XmlElement xe1 = xmlDoc.CreateElement("error"); //创建一个<error>节点
xe1.SetAttribute("time", Convert.ToString(DateTime.Now)); //设置该节点time属性
XmlElement xesub1 = xmlDoc.CreateElement("url");
xesub1.InnerText = context.Request.Url.AbsoluteUri; //设置文本
xe1.AppendChild(xesub1); //添加到<error>节点中
XmlElement xesub2 = xmlDoc.CreateElement("msg");
xesub2.InnerText = context.Server.GetLastError().GetBaseException().Message;
xe1.AppendChild(xesub2);
root.AppendChild(xe1); //添加到<log>节点中
xmlDoc.Save(filePath);
}
#endregion
public void Dispose()
{
}
}
相关文章推荐
- jQuery ajax全局函数处理session过期后的ajax跳转问题
- jQuery ajax全局函数处理session过期后的ajax跳转问题
- C#调用参数为函数指针的API函数 - 以SetUnhandledExceptionFilter为例编写一个全局异常处理程序
- springMVC3学习(八)--全局的异常处理
- 使用@ControllerAdvice及@ExceptionHandler(value = Exception.class)全局异常处理
- springMVC学习(9)-全局异常处理
- struts2异常处理,global-results定义全局结果处理
- SpringBoot学习——全局异常处理设置(返回JSON)
- PHP session 会话处理函数
- 关于windows的safari浏览器的parseInt()函数处理08和09异常问题
- 《Springboot极简教程》系统异常全局统一处理:@ControllerAdvice plus @ExceptionHandler
- springMVC --全局异常处理(两种方式)
- springMVC 获取request 处理全局session业务
- 使用atexit函数进行异常处理
- SpringMVC配置全局日期转换器,处理日期转换异常
- Android全局异常处理(捕获异常,不弹出程序崩溃)
- Spring Boot 全局异常处理
- springboot全局异常处理
- springmvc全局异常处理
- ssm框架中的全局异常处理