views:

29

answers:

2

Is there a single place where I can track any uncaught exceptions in a asp.net mvc application?

I want to email the error, or write to a file/db.

+1  A: 

For any logging / mailing / whatever you want to do when an exception occurs, you should have a look at the application health configuration (described here) in the Web.config file.

In your case (send a mail when an exception occurs), you would use the SimpleMailWebEventProvider provider with the predefined "All Errors" event, like in this exemple :

<healthMonitoring enabled="false" heartbeatInterval="0">
  <providers>
    <add name="notifyAppDev" type="System.Web.Management.SimpleMailWebEventProvider" to="[email protected]" from="[email protected]" subjectPrefix="[WebEvent Error My Site Blabla]" buffer="false" />
  </providers>
  <rules>
    <add name="appDevEvents" eventName="All Errors" provider="notifyAppDev" profile="Critical" minInstances="1" maxLimit="Infinite" minInterval="00:01:00" custom="" />
  </rules>
</healthMonitoring>

Oh and if you want to use this method don't forget to configure the SMTP server from which the mails will be sent, in the Web.config file.

Shtong
does this work for mvc?
Blankman
Yes, ASP.NET MVC uses the same core than the "classic" ASP.NET and share this functionnality along others.
Shtong
A: 

I recommend ELMAH. There are a number of blog posts on how to incorporate it into your mvc application. The most convenient is to use a filter, as described here: http://www.codecapers.com/post/Error-Handling-in-MVC-with-ELMAH.aspx

ErinH