1

I've set up elmah to pick up webapi exceptions by including an httpfilter for webapi events as described in ELMAH and API controller in MVC4 not logging errors
It works, I can see the errors going to the elmah db, problem is the errorMail module doesn't seem to be able to pick them up, even though it picks up the ones for a normal action without issues
I've removed all error filters, that is not why the emails are not being sent.
I've also tried setting up a special filter for emails by adding a void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs args) method in the global.asax but for webapi exceptions this is never even reached (works fine for non-webapi exceptions)
Anyone else has ran into this and knows how to fix it? I would hate to have to replace the elmah email filter...

1 Answer 1

7

I use Elmah.ErrorSignal.FromCurrentContext().Raise(...) instead of Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(...) because all configured error loggers will have a chance to consume the exception. Hence, exceptions will appear in Elmah.axd, and I will receive an email. If I use Log it only goes to the default error logger, and I don't receive an email.

Aside

Web API 2.1 has improved global error handling. If you can use v2.1 I recommend going this route.

public class UnhandledExceptionLogger : ExceptionLogger
{
    public override void Log(ExceptionLoggerContext context)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(context.Exception);
    }
}

To use it, register it with HttpConfiguration like so:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services
    // ...
    config.Services.Add(typeof(IExceptionLogger), new UnhandledExceptionLogger());

    // Web API routes
    // ...
}
1
  • 1
    Worked great. jasonwatmore.com/post/2014/05/03/… expands on the solution if you are just starting to implement Web API, also check your elmah <errorFilter> if this solution doesn't appear to work. Commented May 2, 2019 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.