diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs b/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs index 04516b7783..ca0283a0aa 100644 --- a/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs +++ b/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; +using System.Reflection; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; @@ -51,9 +52,20 @@ namespace Microsoft.AspNetCore.Hosting.Internal public static void ApplicationError(this ILogger logger, Exception exception) { + var message = "Application startup exception"; + + var reflectionTypeLoadException = exception as ReflectionTypeLoadException; + if (reflectionTypeLoadException != null) + { + foreach (var ex in reflectionTypeLoadException.LoaderExceptions) + { + message = message + Environment.NewLine + ex.Message; + } + } + logger.LogCritical( eventId: LoggerEventIds.ApplicationStartupException, - message: "Application startup exception", + message: message, exception: exception); } diff --git a/src/Microsoft.AspNetCore.Hosting/Startup/ConventionBasedStartup.cs b/src/Microsoft.AspNetCore.Hosting/Startup/ConventionBasedStartup.cs index 87f05881e8..b31f9478d1 100644 --- a/src/Microsoft.AspNetCore.Hosting/Startup/ConventionBasedStartup.cs +++ b/src/Microsoft.AspNetCore.Hosting/Startup/ConventionBasedStartup.cs @@ -2,6 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Reflection; +using System.Runtime.ExceptionServices; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.Extensions.DependencyInjection; @@ -19,12 +21,36 @@ namespace Microsoft.AspNetCore.Hosting public void Configure(IApplicationBuilder app) { - _methods.ConfigureDelegate(app); + try + { + _methods.ConfigureDelegate(app); + } + catch (Exception ex) + { + if (ex is TargetInvocationException) + { + ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); + } + + throw; + } } public IServiceProvider ConfigureServices(IServiceCollection services) { - return _methods.ConfigureServicesDelegate(services); + try + { + return _methods.ConfigureServicesDelegate(services); + } + catch (Exception ex) + { + if (ex is TargetInvocationException) + { + ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); + } + + throw; + } } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Hosting/Startup/StartupExceptionPage.cs b/src/Microsoft.AspNetCore.Hosting/Startup/StartupExceptionPage.cs index e399657ceb..7e6123afd8 100644 --- a/src/Microsoft.AspNetCore.Hosting/Startup/StartupExceptionPage.cs +++ b/src/Microsoft.AspNetCore.Hosting/Startup/StartupExceptionPage.cs @@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.Hosting if (!string.IsNullOrEmpty(typeName) && !string.IsNullOrEmpty(type.Namespace)) { builder.Append($@"at {HtmlEncodeAndReplaceLineBreaks(type.Namespace)}"); - typeName = typeName.Substring(type.Namespace.Length + 1); + typeName = typeName.Substring(type.Namespace.Length); } builder.Append(HtmlEncodeAndReplaceLineBreaks(typeName)); @@ -302,12 +302,12 @@ namespace Microsoft.AspNetCore.Hosting private static IEnumerable FlattenAndReverseExceptionTree(Exception ex) { // ReflectionTypeLoadException is special because the details are in - // a the LoaderExceptions property + // the LoaderExceptions property var typeLoadException = ex as ReflectionTypeLoadException; if (typeLoadException != null) { var typeLoadExceptions = new List(); - foreach (Exception loadException in typeLoadException.LoaderExceptions) + foreach (var loadException in typeLoadException.LoaderExceptions) { typeLoadExceptions.AddRange(FlattenAndReverseExceptionTree(loadException)); }