3

I have an MVC 4 web application. Using ELMAH, and Elmah.Contrib.WebApi, any exceptions that occur within my controllers, or API controllers (or their underlying services) are logged perfectly.

Where I run into trouble is with manually logging of errors. Specifically, inside my Global.asax, I have lots of initialization code (setting up automapper and the like). In order to catch problems which might happen in the initialization code, I have the following:

 protected void Application_Start()
    {
        try
        {
            ControllerBuilder.Current.SetControllerFactory(new ErrorHandlingControllerFactory());
            GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            GlobalConfiguration.Configuration.Filters.Add(new ElmahHandleErrorApiAttribute());

            AutoMapperConfiguration.Configure();
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            DatabaseConfig.Initialize();
            SecurityConfig.Initialize();
        }
        catch (Exception e)
        {
            var context = HttpContext.Current;
            var signal = ErrorSignal.FromContext(context);
            if (signal != null)
            {
                signal.Raise(e, context);
            }
        }
    }

Any exception caught inside the "catch" block will never be logged by ELMAH. Yet, I know that ELMAH is working because any other exceptions that will occur from this point on, inside any controller, will be logged. Therefore I have ruled out problems in the web.config, and the like.

Any suggestions? Thanks.

Whatever Man
  • 506
  • 7
  • 21

2 Answers2

5

When the Applicaiton is executing the code in Application_Start it is still being initialized, and it is not handling any HttpRequest.

So the problem with your code is that HttpContext is null. In fact, the line ´ErrorSignal.FromContext(context);´ will throw an ArgumentNullException.

However, you can still use Elmah when there is no HttpContext (and it will basically log the error message and stack trace) as explained in this question.

So your catch would be:

catch (Exception e)
{
    ErrorLog.GetDefault(null).Log(new Error(e));
}

Hope it helps!

Community
  • 1
  • 1
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112
  • Ok, that makes a ton of sense. Thanks! It does log to SQL now (which is my logging store) but the error does not appear in the ELMAH web log. I think the reason is that when I look in SQL, the "application" is blank, which must be related to the application still being initialized, as you said. But at least I have the error in SQL, which is far better than what I had, which is nothing. During my hours of troubleshooting, I had tried using the syntax above, but I later reverted it because I did not see the error on the error page, and I did not think to look in SQL. Thanks again! – Whatever Man Oct 30 '13 at 20:52
  • Adding an applicationName attribute to the errorLog node in Web.config solved that problem. Now the error is logged in SQL and I see it in the error summary page. Perfect! Thank you. – Whatever Man Oct 30 '13 at 21:03
  • Glad to hear that! Have you tried setting the application name in the web.config? See [this answer](http://stackoverflow.com/a/2196594/1836935) – Daniel J.G. Oct 30 '13 at 21:04
  • Posted my comment at the same time than you, I see you have already updated the web.config :) – Daniel J.G. Oct 30 '13 at 21:04
0

I'm a newbie to Elmah but have had luck with something like this:

catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            return Json(string.Format("Failure: {0}", ex.Message), JsonRequestBehavior.AllowGet);
        }
popdan
  • 471
  • 1
  • 6
  • 11