RSS

ASP .NET Health Monitor and Custom Errors

22 Feb
ASP .NET Health Monitor and Custom Errors

Monitoring is an important part of the project life, ASP .NET provides features that makes 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

  <!-- 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 dont 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

<!--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”




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


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


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 might 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.

Advertisements
 
Leave a comment

Posted by on 22, Feb 2012 in .NET, ASP.NET, MVC, VB.NET

 

add your comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: