Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Application Level Error Handling in ASP.NETASP.NET provides many different ways for error handling. Error handling starts on page level with try-catch blocks and Page_Error procedure. To find out more about this see Errors and Exceptions in ASP.NET tutorial. This tutorial goes one step further and explains error handling on Application level. This includes handling errors with Application_Error procedure in Global.asax or by using custom http module. These methods are usually used to log error details in text file, database or Windows EventLog, or to send notification e-mails to administrator. Handling errors in Application_Error in Global.asaxGlobal.asax is optional file. Your site can work without it, but it could be very useful. You can have only one Global.asax in the root folder of your web site. Global.asax contains Application_Error procedure which executes whenever some unhandled error occurs. By using Application_Error, you can catch all unhandled errors produced by your web site, and then write a code to save error messages to database, text file or Windows EventLog, send notificationn e-mail, write some message to user, redirect user to other page with Response.Redirect etc. If you used Server.ClearError() in Page_Error procedure, Application_Error in Global.asax will not execute. Implementation code is similar to Page_Error code above: [ C# ]
void
Application_Error(object sender,
EventArgs e)
[ VB.NET ]
Sub
Application_Error(ByVal sender
As Object,
ByVal e As
EventArgs)
Writing errors to EventLogOne of the ways to track errors is to write them to EventLog. If your web site is on shared hosting you probably can't write to EventLog because of security issues. If you are on dedicated server, you need to enable "Full control" to EventLog for ASPNET account. You can do this in Registry Editor. Go to Start menu -> Run... and type regedt32 or regedit and press Enter. Registry Editor will show. Go to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\EventLog\, right mouse click and select Permissions... from context menu, like in image bellow. Registry editor Click on Permissions... item to show a dialog. Give Full Control to your ASP.NET Machine Account, like in next image (include child objects too!). Registry permissions dialog Now ASPNET account has enough rights. To write in EventLog we can use Try...Catch syntax or Page_Error event, but to avoid duplication of code better option is to use Application_Error in Global.asax file. Code inside Application_Event procedure will be executed every time when some error occurs on web site. [ C# ]
void
Application_Error(object sender,
EventArgs e)
[ VB.NET ]
Sub
Application_Error(ByVal sender
As Object,
ByVal e As
EventArgs) Reading web site error messages from EventLogAfter we created procedure for writing site error messages to Windows EventLog, we need to read them too. You can read these messages by using Windows Event Viewer, located in Control Panel. Also, you can show these messages on web page in some kind of report. To read EventLog error messages with ASP.NET and show them on page you can use code like this: [ C# ]
using
System; [ VB.NET ]
Imports
System Logging of unhandled errors to text file or databaseOn the same way, we can use Application_Error in Global.asax to write error message to text file or in some table in database. In this example, Application_Error procedure contains a code that writes error information to .txt file. [ C# ] void Application_Error(object sender, EventArgs e) [ VB.NET ] Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) Better error logging with Log4NetIf you simply write error details to text file you will get simple, but not scalable solution. In case of large number of concurrent users, text file could be locked for writing and you will get another error. Log4Net is more scalable solution for this problem. To find out how to use Log4Net see Using NHibernate and Log4Net in ASP.NET 2.0 applications tutorial. How to send e-mail with error detailsYou can place a procedure for sending an email inside Application_Error, try-catch block or any other error handling code. Function to send email is pretty simple. In this example, code for sending notification e-mails is located in Application_Error procedure in Global.asax file. On this way e-mail will be sent whenever some undhandled error occurs. [ C# ]
<%@
Application Language="C#"
%> [ VB.NET ]
<%@
Application Language="VB"
%> Handling errors with custom HttpModuleCustom Http module advantage is that module doesn't require changes in your web application code. Http module will catch the same event like Application_Error procedure. To create custom Http Module, start new project in Visual Studio. Project type should be Class Library. Add code like this: [ C# ] using System; [ VB.NET ] Imports System To use the module in web application, you need to add few lines in Web.Config file. <httpModules> More about how to create and implement custom Http modules in ASP.NET see in How To Create Your Own Http Module tutorial. ASP.NET Error Handling RemarksBe careful to handle possible errors in your Application_Error procedure. This procedure is executed when every error occurs, so Application_Error will call itself and it is possible to go in to infinite loop. Writing error logs to text file can cause a problem with concurrency if a lot of errors need to be logged simultaneously. To make this task easier, you can use Log4Net. Also, there is pretty nice complete solution called Elmah now available at Google code. Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | Google |