Monday, 28 January 2013

Sending and Logging Custom Errors in ASP.Net MVC

When you set the custom errors as remote only , you basically want to use the errors for debugging and fixing the problems.

There are various ways how you can use the very useful information from custom errors and notify these errors to the website administrators within no time.

Notifications can happen in many different ways out of which following are the most used:
a. Use Event Log on the server system.
b. Use log tables in SQL Server to log errors.
c. Write error log on to a text file and save them in folders.
d. Send Email to admin(s) with relevant information.
e. Send pager/sms with relevant information to the admin/maintenance team.

Notification of the error can be sent to the administrator in one of the following two ways:
1) Error can be registered as a log entry in the Windows Event Log on the administrator's machine
 2) An Email can be sent to the administrator with a suitable error message
Writing to the Event Log

In ASP.NET, error can be handled programmatically by writing appropriate code in the page-level error event, for errors on an individual page or in the application-level error event for handling errors that may occur in any page of the application.
Therefore, code for writing in the Event Log should be written in either of the events, depending on the requirement of the application. To illustrate this example, I have written the code in the application-level event with the error mode set to "RemoteOnly" and the defaultRedirect attribute to error.htm. The application-level error event should be included in the global file global.asax within the same application folder.

Event Log support is provided in .NET through the namespace System.Diagnostics.

SMTP mail service support is provided in .NET through the namespace System.Web.Mail.

To Log the errors into SQL, create a log table and use ADO.Net classes to write the errors.

To Log error on to text file use System. IO

To send message use System.Net

Send a mail across to me in case you want to implement any of these methods but are confused/need help to implement this.


No comments:

Post a Comment