简体   繁体   中英

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.

I just want the url/line number to appear somewhere inside a prettier custom error page. That way our code isn't exposed when an error is thrown, but I can still find the error quickly by url/line number.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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