日期:2014-05-17  浏览次数:20429 次

.net捕获异常时,怎么输出具体位置呢?
系统默认的异常页面 信息很全,
异常详细信息: System.IO.FileNotFoundException: D:\csharp\webcuo\UploadImg\10323.JPG

源错误: 

行 25: { 
行 26: System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
行 27: int towidth = width;
 

源文件: d:\csharp\webcuo\App_Code\ImageZoom.cs 行: 26 


我现在通过Error.aspx捕获异常
 Exception ex = Server.GetLastError().GetBaseException();
怎么获取上面的信息呢?
ex.Message;ex.Source; 都不对啊

------解决方案--------------------
Server.GetLastError(),是在IIS里捕获错误对象,
所以没有堆栈信息

另外如果try要完整的的堆栈

直接throw e,不要,再new
------解决方案--------------------
写在Global.asax里的protected void Application_Error(object sender, EventArgs e)里

protected void Application_Error(object sender, EventArgs e)
{
Exception erroy = Server.GetLastError();
string err = string.Empty;
err += "错误日期:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n"; ;
err += "出错页面:" + Request.Url.ToString() + "\r\n";
err += "异常信息:" + erroy.Message + "\r\n";
err += "Source:" + erroy.Source + "\r\n";
err += "StackTrace:" + erroy.StackTrace + "\r\n\r\n";
err += "-----------------------\r\n\r\n";
Server.ClearError();
File.AppendAllText(Server.MapPath("error.txt"), err, Encoding.UTF8);
}
输出结果
错误日期:2011-09-05 20:17:01
出错页面:http://localhost:4009/Login.aspx
异常信息:引发类型为“System.Web.HttpUnhandledException”的异常。
Source:System.Web
StackTrace: 在 System.Web.UI.Page.HandleError(Exception e)
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
在 System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
在 System.Web.UI.Page.ProcessRequest()
在 System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
在 System.Web.UI.Page.ProcessRequest(HttpContext context)
在 ASP.login_aspx.ProcessRequest(HttpContext context) 位置 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\99d82696\9a129c9a\App_Web_dbkhly__.0.cs:行号 0
在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)