User Tools

Site Tools

Diagnostics in ASP.NET

Custom error pages in web.config:

<customErrors mode="On" defaultRedirect="CustomErrorPage.htm">
    <error statusCode="400" redirect="CustomErrorPage400.htm"/>
    <error statusCode="404" redirect="CustomErrorPage404.htm"/>
    <error statusCode="500" redirect="CustomErrorPage500.htm"/>

Enable tracing in web.config but prevent trace messages from being displayed on pages. To view the trace messages, browse to the Trace.axd virtual file in the web application's root directory:

<!-- Example #1 -->
<trace enabled="true" pageOutput="false" localOnly="false" />
<!-- Example #2 -->
<trace enabled="true" pageOutput="false" localOnly="false" requestLimit="10" traceMode="SortByCategory" />

Enable tracing on a web page:

<%@ Page Trace="true" TraceMode="SortByTime" %> 

Enable tracing in web.config:


Override application-wide error pages by a page-specific error page:

<%@ Page ... ErrorPage="PageSpecificErrorPage.aspx" %>

Handle unhandled exceptions in Global.asax:

void Application_Error(object sender, EventArgs e)
    Exception exc = Server.GetLastError().GetBaseException();
    // Log the error using a custom method LogError()
    LogError("An unhandled exception occured", exc);
    // Clear the error so it won't be re-thrown.
    // Redirect to a user-friendly page and inform about an internal error.
notes/ · Last modified: 2020/08/26 (external edit)