在我们开发系统时,一般都会记录日志信息,这样方便日后进行维护,同时如果系统出现了错误,也会方便查找,很多
系统开发时都会使用成熟的日志组件,如log4net。但是我今天要介绍的不是日志组件,而是在某些特别的情况下,我们没有
能捕获错误该怎么办???
正如标题所说的,我们可以在Global文件的Application_Error中对错误进行捕获,并记录下来。
下面就来看看下面一段示例代码:
protected void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行,获取错误
Exception objErr = Server.GetLastError().GetBaseException();
if (objErr != null)
{
string error = "Error Page: " + Request.Url.ToString() + "<br>";
if (objErr.Message != null)
{
error += "Error Message: " + objErr.Message + "<br>";
}
if (objErr.StackTrace != null)
{
error += "Stack Message:" + objErr.StackTrace + "<br>";
}
if (objErr.InnerException != null)
{
error += "InnerException" + objErr.InnerException.Message + "<br>";
}
string strSystemLog = "GlobalSystemLog.Log";
string strSystemLogpath = Server.HtmlEncode(strSystemLog);
FileInfo fi = new FileInfo(strSystemLogpath);
if (File.Exists(strSystemLogpath))
{
using (StreamWriter sw = fi.AppendText())
{
sw.WriteLine();
sw.WriteLine(DateTime.Now.ToShortTimeString() + "\n" + error + "\n");
sw.Close();
}
}
else
{
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine();
sw.WriteLine(DateTime.Now.ToShortTimeString() + "\n" + error + "\n");
sw.Close();
}
}
Server.ClearError();
Application["error"] = error;
//跳转到系统出错页面
Response.Redirect("~/SystemError.aspx");
}
}
分享到:
相关推荐
本文为大家介绍下利用Global.asax的Application_Error实现错误记录,具体如下,有此需求的朋友可以参考下,希望对大家有所帮助
ASP.NET错误处理方法Application_Error事件举例如下: 新建web程序——新建AppEvent.aspx页面——在该页面中添加如下代码: 代码如下:[removed]void Page_Load(object sender, System.EventArgs e){throw(new ...
为应用程序在Global.asax文件中创建Application_Error过程,来控制ASP.net代码错误; 为应用程序在Web.config文件中创建Application_Error过程,来控制ASP.net代码错误; 为应用程序在Global.asax文件中创建...
global.asax 用法介绍 <%@ Application Language="C#" %> void Application_Start(object sender, EventArgs e) { // Code that runs on application startup string st= HttpContext.Current.Request....
本文实例讲述了ASP.NET中Global和URLReWrite用法。分享给大家供大家参考。具体如下: Global.asax: 有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级...② Application_Error :程序中发生未
Global.asax.cs特殊事件: Application_Start 网站启动时候触发,只执行一次 Application_End 网站关闭时候触发,只执行一次 Session_Start 创建session时候触发 Session_End 销毁session时候触发 Application_Error...
在文件的最后加入Nand Flash的初始化函数,该函数在后面Nand Flash的操作都要用到。 u-boot运行到第2阶段会进入start_armboot()函数。其中nand_init()函数是对nand flash的最 初初始化函数。nand_init()函数在两个...
} 2)Global.cs中的代码 代码如下: protected void Application_Error(object sender, EventArgs e) { //获取异常信息 string strError = Server.GetLastError().ToString(); //可以将错误信息通过log4ne
The Kernel application is the first application started. It is mandatory in the...• error_logger • file • global • global_group • heart • inet • net_kernel • os • pg2 • rpc • seq_trace • user
代码如下:void Application_Error(object sender, EventArgs e) { if(Context != null) { HttpContext ctx = HttpContext.Current; Exception ex = ctx.Server.GetLastError(); HttpException ev = ex as ...
更新内容: 在原来的版本中, 如果一个路径没有在struts-config.xml文件中配置, 返回的是一个404错误页面, 现在, 如果一个请求路径没有在标签中配置, 我们可以自定义返回的错误页面. 使用示例: 如果你想要使用这个...
目前在开发中用于spa应用程序的技术堆栈。结构体这是库的结构。它与焦点组件一起使用。结构: index.js //entry point__application/ //All application related functions/ helper__definition/ // all the domain ...
在对内存块进行了 free 调用之后,我们需要做的是诸如将它们标记为未被使用的等事情,并且,在调用 malloc 时,我们要能够定位未被使用的内存块。因此, malloc 返回的每块内存的起始处首先要有这个结构: 清单 3...
非特权用户访问时,会出现以下提示: ERROR: ORA-01035: ORACLE 只允许具有 RESTRICTED SESSION 权限的用户使用 6、startup force 强制启动方式 当不能关闭数据库时,可以用startup force来完成数据库的关闭 先...
6.windows环境下在php.ini中加入extension=php_pdo.dll和extension=php_pdo_mysql.dll,在linux环境下编译php时必须加入PDO和PDO_MYSQL模块。 7.在本地测试的环境,还需要到C:\WINDOWS\system32\drivers\etc\host...
在这个应用程序的Global.asax文件中创建一个Application_Error过程去处理ASP.NET代码错误。 B.在这个应用程序的Web.config文件中创建一个applicationError节去处理ASP.NET代码错误。 C.在这个应用程序的Global....
if (ApplicationConfiguration.EventLogEnabled)LogEvent("出现一个未知错误。"); } public AppException(string message) { LogEvent(message); } public AppException(string message,Exception ...
Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理 10.清空Cookie Cookie.Expires=[DateTime]; Response.Cookies(...
调用之后,我们需要做的是诸如将它们标记为未被使用的等事情,并且,在调用 malloc 时,我们要能够定位未被使用的内存块。因此,malloc 返回的每块内存的起始处首先要有这个结构: 清单 3. 内存控制块结构定义 ...