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