global文件里Application_Error方法处理记录应用程序错误日志
2010-07-09 16:42
741 查看
在global文件里的 Application_Error方法中处理,用此方法可以记录应用程序在什么时间发生了什么错误
代码
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex=Server.GetLastError().GetBaseException();
string errorTime="发生时间:"+DateTime.Now.ToString();
string errorAddress="发生异常页:"+Request.Url.ToString();
string errorInfo="异常信息:"+ex.Message;
string errorSource="错误源:"+ex.Source;
string errorTrace="堆栈信息:"+ex.StackTrace;
Server.ClearError();
System.IO.StreamWriter writer=null;
try
{
lock(this)
{
//写入日志
string year=DateTime.Now.Year.ToString();
string month=DateTime.Now.Month.ToString();
string day=DateTime.Now.Day.ToString();
string path=string.Empty;
string filename=DateTime.Now.ToString("yyyyMMdd")+".txt";
path=Server.MapPath("~/Error/")+year+month+day;
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
System.IO.FileInfo file=new FileInfo(path+"/"+filename);
writer=new StreamWriter(file.FullName,true);//文件不在则创建,true表示追加
writer.WriteLine("用户IP:"+Request.UserHostAddress);
writer.WriteLine(errorTime);
writer.WriteLine(errorAddress);
writer.WriteLine(errorInfo);
writer.WriteLine(errorSource);
writer.WriteLine(errorTrace);
writer.WriteLine("-------------------------------------------------------");
}
}
finally
{
if(writer!=null)
{
writer.Close();
}
}
Server.Transfer("~/ErrorPage.aspx"); //跳转到显示友好错误的页面
}
代码
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex=Server.GetLastError().GetBaseException();
string errorTime="发生时间:"+DateTime.Now.ToString();
string errorAddress="发生异常页:"+Request.Url.ToString();
string errorInfo="异常信息:"+ex.Message;
string errorSource="错误源:"+ex.Source;
string errorTrace="堆栈信息:"+ex.StackTrace;
Server.ClearError();
System.IO.StreamWriter writer=null;
try
{
lock(this)
{
//写入日志
string year=DateTime.Now.Year.ToString();
string month=DateTime.Now.Month.ToString();
string day=DateTime.Now.Day.ToString();
string path=string.Empty;
string filename=DateTime.Now.ToString("yyyyMMdd")+".txt";
path=Server.MapPath("~/Error/")+year+month+day;
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
System.IO.FileInfo file=new FileInfo(path+"/"+filename);
writer=new StreamWriter(file.FullName,true);//文件不在则创建,true表示追加
writer.WriteLine("用户IP:"+Request.UserHostAddress);
writer.WriteLine(errorTime);
writer.WriteLine(errorAddress);
writer.WriteLine(errorInfo);
writer.WriteLine(errorSource);
writer.WriteLine(errorTrace);
writer.WriteLine("-------------------------------------------------------");
}
}
finally
{
if(writer!=null)
{
writer.Close();
}
}
Server.Transfer("~/ErrorPage.aspx"); //跳转到显示友好错误的页面
}
相关文章推荐
- Global.asax的Application_Error实现错误记录/错误日志的代码
- [置顶] 利用Global.asax的Application_Error实现错误记录,错误日志
- 在系统出现未处理的错误时,在Global的Application_Error记录下错误
- Global.asax的Application_Error实现错误记录/错误日志的代码
- 利用Global.asax的Application_Error实现错误记录,错误日志
- 在系统出现未处理的错误时,在Global的Application_Error记录下错误
- archive log 日志已满 ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
- 网狐荣耀版或其它安卓项目出现Application cannot be exported due to the error(s) below.错误的处理方法
- 在Global中Application_Error事件处理错误信息
- Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)
- 11.在Global的Application_Error处理错误示例
- ORA-00257 archiver error. 归档日志已满错误的处理方法
- IIS发布网站浏览之后看到的是文件目录 & Internal Server Error 处理程序“ExtensionlessUrlHandler-ISAPI-4.0_64bit”在其模块列表中有一个错误模块“IsapiModule” 解决方法 & App_global.asax.pduxejp_.dll”--“拒绝访问。 ”
- 在Global中Application_Error事件处理错误信息
- “/”应用程序中的服务器错误和Server Error in ''/'' Application.的终极解决方法
- 解决 Global.asax的Application_Error中记录"文件不存在"的问题
- ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法(日志已满)
- (日志空间满了):ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
- ASP.NET错误处理方法归结(方法2):Application_Error 事件
- 使用Application_Error事件处理程序把异常记录到系统事件日志