0
votes

I am working with WebApi and ELMAH and I would like to be able to wrap some business errors into web responses for some thypes of exceptions(no logging) and have the others logged by ELMAH. Right no I have this piece of code.

public async override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
        {
            try
            {
            return await base.ExecuteAsync(controllerContext, cancellationToken);
            }
            catch (MyException ex)
            {
                return new HttpResponseMessage(CustomCode)
                {
                    Content = new StringContent(CustomMessage)
                };
            }          
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                return new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent("Ups! Something went wrong. Please try again or contact administrator!")
                    };
            }
        }

The problem is I would expect ELMAH to log only unhandled exceptions. In my case, even the exception of type MyException is being logged, although it is caught by the catch block.

Is there any config I can apply to ELMAH to make it log only unhandled eceptions ?

1

1 Answers

0
votes

ELMAH catches all exceptions thrown from your web app. If you want to filter out certain errors, you could the error filtering feature of ELMAH as described here:

https://code.google.com/p/elmah/wiki/ErrorFiltering

In your case you would probably need to add something like this to your global.asax.cs:

void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
    if (e.Exception.GetBaseException() is MyException)
        e.Dismiss();
}