Introduction
Getting full error tracking can be tricky because there are various different scenarios to consider that prevent LeanSentry from capturing exceptions fully.
How IIS/ASP.NET error handling works
When the ASP.NET application code throws an exception, there could be two possible scenarios:
- The code handles it
There are multiple places where code can handle these exceptions:-
Page.Error event
-
Application.Error event (for all errors in the application)
-
For an ASP.NET MVC application, there is a HandleErrorAttribute that can be attached to a controller or action that can handle these errors.
-
- The exception remains unhandled
- If the exception is unhandled, it bubbles out to the top of the ASP.NET "stage".
-
ASP.NET then:
-
Logs an event to the EventLog for the error (where we get the error from)
-
Applies any custom error pages, or sends the full error response to the user
-
-
- If the exception is unhandled, it bubbles out to the top of the ASP.NET "stage".
Factors affecting LeanSentry's ability to track errors
The following issues affect our ability to pick up those errors:
- If the application intercepts the error and handles it by:
- Using a try/catch block and catching the error in the code directly.
- Catches the error in Page.Error, or Application.Error, and calls Server.ClearError() to remove the error from the request. This is often done in API applications that want to return JSON errors for example, or want to provide a specific error response, or want to log the error to a custom error log.
SOLUTION:
Reference ApplicationMonitoring.dll, and call Monitoring.LogError(HttpContext, Exception) wherever you handle exceptions yourself.
TIP: Do this from the existing application-wide error logging helper if possible. This way, a customer can add 1 line of code to their main helper method for logging exceptions, and get full error tracking from any place that calls their error logging path.
- Using a try/catch block and catching the error in the code directly.
- ASP.NET MVC default HandleErrorAttribute handles errors and does not log them to the EventLog.
- SOLUTION:
ApplicationMonitoring.Automatic.Mvc.dll. It injects a special error filter BEFORE the HandleErrorAttribute, and logs those errors automatically
- SOLUTION:
- By default, ASP.NET will only log 1 occurrence of each error every minute or so.
- As a result, we have incorrect counts of errors reported where you might have 1 NullReferenceException in /Login but 250 IIS 500 errors due to it.
SOLUTION:
Enable "Allow LeanSentry to set IIS configuration". Because we already set "Track all ASP.NET exceptions" by default, all we need is the permission to set the configuration on their server and we'll start counting errors correctly.
- As a result, we have incorrect counts of errors reported where you might have 1 NullReferenceException in /Login but 250 IIS 500 errors due to it.
- WCF services hosted in IIS do not report unhandled exceptions the same way that ASP.NET applications do.
- In order to enable error reporting for WCF applications:
- Intercept the error in your service: http://msdn.microsoft.com/en-us/library/gg281715.aspx. You may already have this set up, or you can easily add it using the IErrorHandler pattern or the Faulted event on the ServiceHost class.
- Raise an error event via the ASP.NET health monitoring system.
This is how a normal ASP.NET application logs unhandled exceptions. (see https://leansentry.zendesk.com/entries/23584078-How-do-I-enable-ASP-NET-detailed-error-tracking- for more):
Create an error event wrapper:
public class MyRequestErrorWebEvent : WebRequestErrorEvent { public const int CustomEventCode = 100801; public MyRequestErrorWebEvent(string msg, Exception e) : base(msg, null, CustomEventCode, e) { } }
Then when you get an exception:// Raise the error to health monitoring new MyRequestErrorWebEvent("An unhandled request exception has occurred", exception).Raise();
- Intercept the error in your service: http://msdn.microsoft.com/en-us/library/gg281715.aspx. You may already have this set up, or you can easily add it using the IErrorHandler pattern or the Faulted event on the ServiceHost class.
- In order to enable error reporting for WCF applications:
After making the suggested changes, LeanSentry will be able to capture all application errors.
Comments
0 comments
Please sign in to leave a comment.