It's very important to log any uncaught exceptions that your ASP.NET application generates. Otherwise you may not be aware of serious problems with your website. You can trap exceptions by writing an Application_Error method in Global.asax. Call Server.GetLastError() to get the exception.I recommend logging the following data so that you'll be able to troubleshoot the problem:
| Data |
How to Retrieve |
| Current Time |
DateTime.Now |
| Request IP Address |
Request.UserHostAddress |
| Referring URL (page user was on when the current page was requested) |
Request.UrlReferrer |
| User Agent (browser or spider that issued the request) |
Request.UserAgent |
| User Host Name |
Request.UserHostName |
| All HTTP Headers |
Request.Headers |
| All Form Fields |
Request.Form |
| Exception Details |
ex.GetType(), ex.Message, ex.StackTrace, where ex is the exception returned by Server.GetLastError() |
| Inner Exception Details |
Same as above, if ex.InnerException is not null. |
If you find yourself getting spurious entries in your exception log that you'd like to suppress, I suggest putting regular expressions in your web.config file that match the entries that you're not interested in. When an uncaught exception occurs, generate the log entry, but then see if any of the regular expressions match the log entry. If any do, don't save the log entry.