Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET Health Monitor and Custom Errors

0.00/5 (No votes)
22 Feb 2012 2  
Enable email notifications on errors, and create custom pages for handling errors in MVC

Monitoring is an important part of the project life, ASP.NET provides features that make it easy to monitor your web app while it is in production, it is called ASP.NET Health Monitoring.

Enabling Health Monitor

Create a new ASP.NET MVC 3 Web Application and edit the Web.Config file, add the following between the <system.web> tags:

XML
<!-- Health Monitoring -->
  <healthMonitoring enabled="true">
    <providers>
      <clear />
      <!-- Email WebEvents Provider -->
      <add name="EmailWebEventProvider"
  type="System.Web.Management.SimpleMailWebEventProvider"
  from="logger@mydomain.com" to=MY_USERNAME@gmail.com
  bodyHeader="My Monitor" buffer="false" />
    </providers>
    <eventMappings>
      <clear />
      <add name="All Errors" type="System.Web.Management.WebBaseErrorEvent"
      startEventCode="0" endEventCode="2147483647" />
    </eventMappings>
    <rules>
      <clear />
      <add name="All Errors Default" eventName="All Errors"
  provider="EmailWebEventProvider" profile="Default" minInstances="1"
  maxLimit="1000" minInterval="23:59:00" />
    </rules>
  </healthMonitoring>

Here we have enabled health monitoring with an Email Alert for any error that occurs but we will only be notified once a day for each specific error, as we have set minInterval=”23:59:00?.

To complete the configuration, go ahead and set the “from”, “to”, and “header” properties in the above code, for more details on the health monitor properties, go here.

GMAIL SMTP Server

At this point, it will use the default local host smtp server, if you don't have an SMTP server running, you can set it up to connect to any available server, here we are using gmail SMTP server as I have a gmail account (500 email/day limit).

Add the following after the <system.web> tags:

XML
<!--SMTP -->
  <system.net>
    <mailSettings>
      <smtp from="noreply@mydomain.com" deliveryMethod="Network">
        <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" 
    password="MY_PASSWORD" port="587" userName="MY_USERNAME@gmail.com" />
      </smtp>
    </mailSettings>
  </system.net>

Enter your email and password in the “password” and “userName” fields.

ERROR DEMO

To trigger an error, we will add the following code to the “HomeController”:

VB.NET
Function Index() As ActionResult
ViewData("Message") = "Welcome to ASP.NET MVC!"

Throw New ArgumentException("ERROR FROM HomeController")

Return View()
End Function

Open the site and you will get an error, and an email notification.

Handling Errors

To suppress these errors and show our own custom page, we have to do some extra work, in our global.asax file, we add a global error handler, then we create views for each of the different types of errors you want to have a custom page for.

Open Global.asax and add the following code:

VB.NET
Private Sub MvcApplication_Error(sender As Object, e As System.EventArgs) _
    Handles Me.Error
Dim routeData = New RouteData
routeData.Values.Add("controller", "Home")
Dim lastEx = Server.GetLastError
If TypeOf (lastEx) Is HttpException Then
Dim ex As HttpException = lastEx
Select Case ex.GetHttpCode
Case 404
routeData.Values.Add("action", "Error404")
Case 500
routeData.Values.Add("action", "Error500")
Case Else
routeData.Values.Add("action", "ErrorGeneric")
End Select
Else 'not an Http Ex.
routeData.Values.Add("action", "ErrorGeneric")
End If
routeData.Values.Add("exception", lastEx)
Server.ClearError()
Dim errController As IController = New ErrorController
errController.Execute(New RequestContext(New HttpContextWrapper(Context), routeData))
End Sub

In the above code, we handle the Me.Error global event, then we choose which view we want to show based on the HttpCode, and Exception type, we also put our Exception in routeData (optional) so it will be available to us in our View, clear the error that tells ASP.NET that we handled it, and then return one of the Views from the ErrorController. Let’s create the ErrorController.

ErrorController

Create a new controller, name it “ErrorController”, and add the following code:

VB.NET
Public Function Error404() As ActionResult
Response.StatusCode = 404
Dim ex As Exception = Nothing
If RouteData.Values.ContainsKey("exception") Then
ex = RouteData.Values("exception")
End If
Return View(ex)
End Function
Public Function Error500() As ActionResult
Response.StatusCode = 500
Dim ex As Exception = Nothing
If RouteData.Values.ContainsKey("exception") Then
ex = RouteData.Values("exception")
End If
Return View(ex)
End Function
Public Function ErrorGeneric() As ActionResult
Dim ex As Exception = Nothing
If RouteData.Values.ContainsKey("exception") Then
ex = RouteData.Values("exception")
End If
Return View(ex)
End Function

Here we have 3 views, one for 404 (Not Found), another for 500 server error, and a default generic error handler. We inspect if we passed in the exception in routeData and pass it to our view as the view model type.

Right click on the “Shared” folder and create the following 3 views.

Error404.vbhtml

@ModelType Exception
@Code

ViewData("Title") = "Error 404 : Page not found"

End Code
@ViewData("Title")
My Custom Error Page

@Model.Message

@Model.StackTrace

Error500.vbhtml

Save as above.

ErrorGeneric.vbhtml

Save as above.

Now if we rebuild and open the site again, we will see the following screen:

Screenshot

At this point, your site has been setup to email you when errors occur in your app, and if they do, the visitor will see a nice custom error page. With this in your app, you can handle more complex scenarios like having a 404 page that inspects the Exception passed in and tries to figure out what the visitor was trying to find in our site and give them some suggestions.

You can download the full sample project here.

I hope you like this post and for my next post, I will be looking at creating a whole site using ASP.NET localization features.


Filed under: .NET, ASP.NET, MVC, VB.NET

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here