Doug
Doug

Reputation: 6518

Catching an exception and clearing it before ELMAH gets it

I have a problem where I have an exception being thrown that I am capturing in Global.asax. As part of this exception handling I redirect the user to a specific page because of this exception.

I also have ELMAH error handling with the email module plugged in. I do not want to receive emails for this exception. I also don't want to add this type of exception to ELMAHs ignore list, in case I want to do granular work around the exception (i.e., only if it matches certain properties, happens on certain pages)

I want to:

I am currently calling Server.ClearError() inside my App_OnError method, but am still receiving these emails.

Upvotes: 2

Views: 1372

Answers (2)

sajoshi
sajoshi

Reputation: 2763

Are you using [HandleError] attribute as well? The way I would do is:

  1. Create a custom HandleError attribute for action level and will not apply the attribute on the action where I don't want to get ELMAH involved.
  2. The Application_Error code will exists as is.

Upvotes: 0

Doug
Doug

Reputation: 6518

As per the ELMAH documentation

I put leave my Application_OnError method in my Global.asax but I also add the following global methods:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
    Filter(args);
}

void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs args)
{
    Filter(args);
}

void Filter(ExceptionFilterEventArgs args)
{
    if (args.Exception.GetBaseException() is HttpRequestValidationException)
    {
        args.Dismiss();
    }
}

What this does is dismiss the ELMAH exception if it is the type that matches what I want - in my case the HttpRequestValidationException.

The ErrorMail_Filtering method is only required if you have the error mail filter turned on - which I do.

Upvotes: 5

Related Questions