private async Task HandleUsingDefaultExceptionHandler(HttpContext context, System.Exception originalException) { var originalExceptionName = originalException.GetType().FullName; if (originalException.GetType() == typeof(TimeoutException)) { logger.LogCritical ( originalExceptionName, "Unhandled exception {ExceptionName} thrown with message {ExceptionMessage}", originalExceptionName, originalException.Message ); } else { logger.LogError ( originalException, "Unhandled exception {ExceptionName} thrown with message {ExceptionMessage}", originalExceptionName, originalException.Message ); } var version = context.GetRequestedApiVersion()?.MajorVersion ?? Versions.Latest; IDefaultExceptionHandler defaultExceptionHandler; try { defaultExceptionHandler = componentContext.ResolveVersioned <IDefaultExceptionHandler>(version); } catch (System.Exception exception) { var exceptionName = exception.GetType().FullName; logger.LogError ( exception, "Exception {ExceptionName} thrown with message {ExceptionMessage} when resolving default exception handler for exception {OriginalExceptionName} with message {OriginalExceptionMessage} for version {Version}", exceptionName, exception.Message, originalExceptionName, originalException.Message, version ); if (hostingEnvironment.IsDevelopment()) { var response = new ExceptionResponse ( "DefaultExceptionHandlerNotRegistered", $"Exception {exceptionName} thrown with message {exception.Message} when resolving default exception handler for exception {originalExceptionName} with message {originalException.Message} for version {version}", new Exception(exception) ); await context.Response.WriteJsonAsync(HttpStatusCode.InternalServerError, response, JsonConstants.JsonSerializerSettings); } else { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; } return; } try { await defaultExceptionHandler.HandleAsync(context, originalException); } catch (System.Exception exception) { var exceptionName = exception.GetType().FullName; logger.LogError ( exception, "Exception {ExceptionName} thrown with message {ExceptionMessage} when handling exception {OriginalExceptionName} with message {OriginalExceptionMessage}", exceptionName, exception.Message, originalExceptionName, originalException.Message ); if (hostingEnvironment.IsDevelopment()) { var response = new ExceptionResponse ( "DefaultExceptionHandlerThrewException", $"Exception {exceptionName} thrown with message {exception.Message} when handling exception {originalExceptionName} with message {originalException.Message}", new Exception(exception) ); await context.Response.WriteJsonAsync(HttpStatusCode.InternalServerError, response, JsonConstants.JsonSerializerSettings); } else { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; } } }