日期:2014-05-18  浏览次数:20409 次

|M| 如何能够自定义一个像ASP.NET出错时的页面 不是一般的自定义 要取出错代码
如:ASP.NET出错的时候会有说哪些代码出错:
源错误:  

行   18:  
行   19:                   Session[ "aa "]   =   "cc ";
行   20:                   Response.Write(Session[ "dd "].ToString());
行   21:           }
 
然后现在我想自己来用一个出错日志文件
我用
System.Web.HttpContext.Current.Response.Write( "关联帮助文件: <br> "   +   Server.GetLastError().HelpLink);
System.Web.HttpContext.Current.Response.Write( "导常实例: <br> "   +   Server.GetLastError().InnerException);
System.Web.HttpContext.Current.Response.Write( "消息描述: <br> "   +   Server.GetLastError().Message);
System.Web.HttpContext.Current.Response.Write( "错误对像名称: <br> "   +   Server.GetLastError().Source);
System.Web.HttpContext.Current.Response.Write( "表示形式: <br> "   +   Server.GetLastError().StackTrace);
System.Web.HttpContext.Current.Response.Write( "异常方法: <br> "   +   Server.GetLastError().TargetSite);
这些方法,但里面没有一个是说上面的那一段代码的
那么我如何在程序里得出
行   18:  
行   19:                   Session[ "aa "]   =   "cc ";
行   20:                   Response.Write(Session[ "dd "].ToString());
行   21:           }
这些呢

谢谢

------解决方案--------------------
1。
Server.GetLastError().StackTrace 属性就包含这个信息

2。
行 18:
行 19: Session[ "aa "] = "cc ";
行 20: Response.Write(Session[ "dd "].ToString());
行 21: }

上面的的错误信息出现在 Server.GetLastError().StackTrace 中的最顶级堆栈调用(方法调用)

假如你仔细观察、对比就会发现
------解决方案--------------------
最顶级堆栈

====

这样理解,是调用链上抛出异常的那个方法的代码行,

StackTrace 以倒序的方式输出,最后被调用的方法出现在最上面,
------解决方案--------------------
protected void Application_Error(object sender, EventArgs e) { Exception er = Server.GetLastError(); if (er != null) { string str = er.Message; Server.ClearError(); string Url = Request.Url.LocalPath; Response.Redirect( "~/Err.aspx?ErrMsg= " + Server.UrlEncode(str) + "&Url= " + Server.UrlEncode(Url)); } }
------解决方案--------------------
Global.asax
------解决方案--------------------
这是微软的工程师说的: 发布出去的网站应该取不到源错误的。Exception里面也的确没有。应该是Debug的关系才会在页面上显示源错误。 < 胡文毅 在线技术支持工程师 微软全球技术支持中心
------解决方案--------------------
哈哈,不清楚怎么回事,没有研究过
------解决方案--------------------
行 18:
行 19: Session[ "aa "] = "cc ";
行 20: Response.Write(Session[ "dd "].ToString());
行 21: }

======

这段代码也是 ASP.NET 引擎通过获取 StactTrace 属性格式化出来的


只有 Debug 版本 CLR 才能检索到代码源文件的行号 ( line ),并附加到堆栈信息中(StackTrace)

而 Release 版本的堆栈调用信息是不包含代码源文件的行号

因为,这需要一定的性能损耗
------解决方案--------------------