Reputation: 3803
Am currently using mvc4 with vs2012 and i installed mvc.elmah nugget and am done.
What i did ?
Initially i didnt write any code and my exception were logged in ELMAH.
Later i decided to use MVC HandleError to handle Application Exceptions and added filter
filters.Add(new HandleErrorAttribute());
Now i debugged in filters and see i have 2 filters one for ELMAH HandleErrorAttribute and MVC HandleErrorAttribute !
I saw this excellent link https://stackoverflow.com/a/5936867/1481690 which talks about using ErrorSignal to handle Application Exceptions
But am not using ErrorSignal
but still my Exceptions are captured by ELMAH ?
If i use
if (context.ExceptionHandled)
ErrorSignal.FromCurrentContext().Raise(context.Exception);
My Exceptions are logged twice.
My Question as follows
What am doing currently is
Added filters
filters.Add(new HandleErrorAttribute());
and overriding (To handle Ajax Error in future)
public class HandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
base.OnException(context);
}
Is this enough or what is the good practise ? Am not raising ErrorSignal as it is logging twice.
Am not using any try.catch
as am using below line and hope my exception is handled.
base.OnException(context);
}
Just want to make sure that am on right way !
Thanks for the time
Upvotes: 3
Views: 1427
Reputation: 16928
UPDATE: I pointed to the wrong package updating question.
You don't need both. ElmahHandleErrorAttribute
inherits from HandleErrorAttribute
, so using both is redundant.
How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?
You also do not need to raise any signals for Elmah, the ElmahHandleErrorAttribute
handles this.
If you're using Elmah.Contrib.MVC from Nuget all you need is ElmahHandleErrorAttribute
.
filters.Add(new Elmah.Contrib.MVC.ElmahHandleErrorAttribute());
There's also a package on there that I mistook for this one at Elmah.MVC done by the same author as Elmah, I'm looking into this one to find out what the differences are and how to use it.
UPDATE 2: Ok, so apparently the Elmah.MVC package has all of this built in, and they register pretty much everything for you using WebActivator. So literally all you need to do on a brand new MVC project to start tracking errors with Elmah is to install the package, and compile. Done. However you should check your web.config after installing it, and under AppSettings, set the role required to view the page and enable the authentication.
Upvotes: 5
Reputation: 9881
I believe the way you have it is correct. This is in my Global.asax.cs
private static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ElmahHandleErrorAttribute());
filters.Add(new HandleErrorAttribute());
}
One thing to check is that your controllers do not have either [ElmahHandleError]
or [HandleError]
attributes.
Upvotes: 2