title: How do I log ALL exceptions globally for a C# MVC4 WebAPI app?
body: <h1>Background</h1>
  
  <p>I am developing an API Service Layer for a client and I have been requested to catch and log all errors globally.</p>
  
  <p>So, while something like an unknown endpoint (or action) is easily handled by using ELMAH or by adding something like this to the <code>Global.asax</code>:</p>
  
  <pre><code>protected void Application_Error()
  {
       Exception unhandledException = Server.GetLastError();
       //do more stuff
  }
  </code></pre>
  
  <p>. . .unhandled errors that are not related to routing do not get logged.  For example:</p>
  
  <pre><code>public class ReportController : ApiController
  {
      public int test()
      {
          var foo = Convert.ToInt32("a");//Will throw error but isn't logged!!
          return foo;
      }
  }
  </code></pre>
  
  <p>I have also tried setting the <code>[HandleError]</code> attribute globally by registering this filter:</p>
  
  <pre><code>filters.Add(new HandleErrorAttribute());
  </code></pre>
  
  <p>But that also does not log all errors.</p>
  
  <h1>Problem/Question</h1>
  
  <p>How do I intercept errors like the one generated by calling <code>/test</code> above so that I can log them?  It seems that this answer should be obvious, but I have tried everything I can think of so far. </p>
  
  <p>Ideally, I want to add some things to the error logging, such as the IP address of the requesting user, date, time, and so forth.  I also want to be able to e-mail the support staff automatically when an error is encountered.  All of this I can do if only I can intercept these errors when they happen!</p>
  
  <h1>RESOLVED!</h1>
  
  <p>Thanks to Darin Dimitrov, whose answer I accepted, I got this figured out.  <em>WebAPI does <strong>not</strong> handle errors in the same way as a regular MVC controller.</em></p>
  
  <p>Here is what worked:</p>
  
  <p>1) Add a custom filter to your namespace:</p>
  
  <pre><code>public class ExceptionHandlingAttribute : ExceptionFilterAttribute
  {
      public override void OnException(HttpActionExecutedContext context)
      {
          if (context.Exception is BusinessException)
          {
              throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
              {
                  Content = new StringContent(context.Exception.Message),
                  ReasonPhrase = "Exception"
              });
  
          }
  
          //Log Critical errors
          Debug.WriteLine(context.Exception);
  
          throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
          {
              Content = new StringContent("An error occurred, please try again or contact the administrator."),
              ReasonPhrase = "Critical Exception"
          });
      }
  }
  </code></pre>
  
  <p>2) Now register the filter globally in the <strong>WebApiConfig</strong> class:</p>
  
  <pre><code>public static class WebApiConfig
  {
       public static void Register(HttpConfiguration config)
       {
           config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
           config.Filters.Add(new ExceptionHandlingAttribute());
       }
  }
  </code></pre>
  
  <p><strong>OR</strong> you can skip registration and just decorate a single controller with the <code>[ExceptionHandling]</code> attribute.</p>