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.