Runtime exceptions and errors happen sometimes (or I should say always :) ), but end users don't need to know the details of them. Also showing internal details of the application may harm the security (gives hackers sensitive information). So websites should show user friendly error pages to users and save-log detailed technical information in an error message. To do so, we can configure web.config:
<customErrors mode="RemoteOnly" defaultRedirect="~/FriendlyErrorMessagePage.aspx">
<error statusCode="404" redirect="~/PageNotFound.aspx" />
</customErrors>
In this example, for error "404" there is a specific page, and all other cases are addressed in "defaultRedirect" attribute of "customErrors". "RemoteOnly" means, when working in localhost, show the error page. But this is not enough. We should also have a system that saves technical details and inform the administrator about them. To take some action in "OnError" event in our code-behind files is a good option. A better approach is, rather than copying this code in every page, we can write a base page that inherits System.Web.UI.Page.
public class MyBasePage : System.Web.UI.Page
{
protected override void OnError(EventArgs e)
{
//log the error-exception
}
//................
}
Another option is doing this in "Application_Error" event in "Global.asax.cs" :
void Application_Error (Object sender, EventArgs e)
{
//log the error-exception
}
This approaches work but there is a full-scale mechanism in ASP.NET to get same functionality: instrumentation and health monitoring system. This system takes care of :
-
unhandled exceptions
-
lifetime exceptions (start of application, finish of application etc.)
-
audit events (failed or successful logins etc.)
-
.....
Instrumentation and health monitoring system is a framework and based on provider model design pattern. So we can log the details to SQL Server Database, the Windows Event Log, the WMI log, to mails or to Oracle RDMS by writing a specific provider for Oracle (by inheriting System.Web.Management.WebEventProvider).
Configuration setting of health monitoring is under <healthMonitoring> element of web.config
<healthMonitoring>
<eventMappings>........</eventMappings>
<providers>.................</providers>
<rules>.......................</rules>
<profiles>....................</profiles>
<bufferModes>.............</bufferModes>
</healthMonitoring>
<eventMappings>........</eventMappings> registers the event classes so their types like:
<eventMappings>
<add name="HeartBits" type="System.Web.Management.WebHeartbeatEvent,System.Web" />
</eventMappings>
<providers>.................</providers> section registers providers:
<providers>
<add name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider,System.Web" ConnectionStringName="localSqlServer" />
</providers>
<rules>.......................</rules> is the place we actually define which events to log, which registered provider and how often. Now an example that HeartBits events to be logged to Sql Server:
<rules>
<add name="HeartBitEvents" eventName="HeartBits" provider="SqlWebEventProvider"
</rules>