A day with .Net

My day to day experince in .net

Exception Logging in Web API using ELMAH.

Posted by vivekcek on September 28, 2013

In this post i will show you how to implement exception logging in Web Api.

1. Install ELMAH Mvc from Nuget.

2. Create an exception filter.

    public class WebApiExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(actionExecutedContext.Exception));
        }
    }

3. Now use this exception filter in your web api methods.

        [HttpGet]
        [WebApiExceptionFilter]
        public string GetName()
        {
            throw new Exception("Test");
           
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s