全局处理函数(处理session、异常等)

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()
{
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值