简体   繁体   English

CustomError页面中的错误信息

[英]Error information in CustomError Page

I want to have a custom error page that displays some error information, but not the stack trace. 我想要一个自定义错误页面,该页面显示一些错误信息,但不显示堆栈跟踪。 I would like for it to display the information for the offending page and the line number. 我希望它显示违规页面和行号的信息。

The default SERVER ERROR IN APPLICATION page shows the url/line number and surrounding code/stack trace. 默认的SERVER ERROR IN APPLICATION页面显示URL /行号和周围的代码/堆栈跟踪。

I just want the url/line number to appear somewhere inside a prettier custom error page. 我只希望url /行号出现在更漂亮的自定义错误页面内。 That way our code isn't exposed when an error is thrown, but I can still find the error quickly by url/line number. 这样,当引发错误时,我们的代码就不会暴露出来,但是我仍然可以通过url /行号快速找到错误。

I already have custom error pages turned on. 我已经打开了自定义错误页面。 I just want to add additional information to them. 我只想向他们添加其他信息。 I'm using C#.NET 4.0 and webforms. 我正在使用C#.NET 4.0和Webforms。

This is a question I have had for a while and for the longest time though it was not possible, so I did some research and found a solution. 尽管这是不可能的,但是这是我有一段时间了,而且时间最长的一个问题,因此我进行了一些研究并找到了解决方案。 You need to use the StackFrame object from the System.Diagnostics namespace. 您需要使用System.Diagnostics命名空间中的StackFrame对象。 I just put together an example and it appears to have worked. 我只是举了一个例子,它似乎奏效了。

try
{
    int a = 10;
    int b = 0;
    litDebug.Text = (a / b).ToString();
}
catch (Exception ex)
{
    StackTrace st = new StackTrace(ex, true);
    StackFrame sf = st.GetFrame(0);
    litDebug.Text = "" +
        "<br />File Name: " + sf.GetFileName() +
        "<br />Method Name: " + sf.GetMethod().Name +
        "<br />Error Line Number: " + sf.GetFileLineNumber() +
        "<br />Error Column Number: " + sf.GetFileColumnNumber();
}

The page output is: 页面输出为:

File Name: c:\inetpub\www.website.com\dev\error.aspx.cs
Method Name: Page_Load
Error Line Number: 17
Error Column Number: 4

The only questionable item is the Error Column Number - everything else matches up perfectly. 唯一可疑的项目是“错误列号”-其他所有内容都完全匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM