Concept of error pages and error logging in ASP.NET

Q.3) Explain the concept of error pages and error logging with the help of suitable example.

Ans.

When an exception or compile-time error occurs in an ASP.NET application it shows a default error page. In this error page, brief description of the error along with the line number is displayed. You can customize the default error page. For customization you have to change the configuration settings in Web.config file.

There are three types of error modes are available in ASP.NET framework.

1) Off
2) On
3) RemoteOnly

When the error mode is set to "Off", ASP.NET uses its default error page for both local and remote users. If the Mode is “On”, ASP.NET uses user-defined custom error page for both local and remote users. In case of RemoteOnly mode, ASP.NET error page is shown only to local users and Custom error pages are shown for all remote users.

For displaying custom error pages change the web.config file according to given below code.

< ?xml version="1.0" encoding="utf-8" ?>
< configuration>
< system.web>
< customErrors defaultRedirect="ErrorDemo.html" mode="On" />
< /system.web>
< /configuration>

If the error generates the defaultRedirect attribute redirects to ErrorDemo.html page.
The contents of the user-defined error page ErrorDemo.html can be given as follows:

< HTML>
< BODY>
< b> We are very sorry. The site is under maintenance...............< br> < /b>
< /BODY>
< /HTML>

Writing to the Event Log

To create an event log, the .NET Framework provides the EventLog class. To use this class, the Source property and a message needs to be specified, as shown in the following code. All the classes related to EventLog are available in System.Diagnostics namespace:
protected void Button1_Click(object sender, EventArgs e)
{
if (!EventLog.SourceExists("MyAppSource"))
{
EventLog.CreateEventSource("MyAppSource", "System");
}
EventLog.WriteEntry("MyAppSource", "Event Log Called...",
EventLogEntryType.Information);
}
Post your comment