[Fixes #819] Display LoaderExceptions in ReflectionTypeLoadException

This commit is contained in:
Ajay Bhargav Baaskaran 2016-07-20 17:57:35 -07:00
parent b955ec7743
commit 184a25792a
3 changed files with 44 additions and 6 deletions

View File

@ -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);
}

View File

@ -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;
}
}
}
}

View File

@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.Hosting
if (!string.IsNullOrEmpty(typeName) && !string.IsNullOrEmpty(type.Namespace))
{
builder.Append($@"<span class=""faded"">at {HtmlEncodeAndReplaceLineBreaks(type.Namespace)}</span>");
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<Exception> 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<Exception>();
foreach (Exception loadException in typeLoadException.LoaderExceptions)
foreach (var loadException in typeLoadException.LoaderExceptions)
{
typeLoadExceptions.AddRange(FlattenAndReverseExceptionTree(loadException));
}