From dc35157f7a66b17df0267bdfdc4673631dedb033 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 30 Jun 2016 10:59:17 -0700 Subject: [PATCH] Use MethodDisplayInfo to display stack trace --- .../Startup/StartupExceptionPage.cs | 55 +++++-------------- 1 file changed, 13 insertions(+), 42 deletions(-) diff --git a/src/Microsoft.AspNetCore.Hosting/Startup/StartupExceptionPage.cs b/src/Microsoft.AspNetCore.Hosting/Startup/StartupExceptionPage.cs index 32fac13137..e399657ceb 100644 --- a/src/Microsoft.AspNetCore.Hosting/Startup/StartupExceptionPage.cs +++ b/src/Microsoft.AspNetCore.Hosting/Startup/StartupExceptionPage.cs @@ -12,7 +12,6 @@ using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; using System.Text; using System.Text.Encodings.Web; -using Microsoft.Extensions.Internal; using Microsoft.Extensions.StackTrace.Sources; namespace Microsoft.AspNetCore.Hosting @@ -81,6 +80,7 @@ namespace Microsoft.AspNetCore.Hosting var builder = new StringBuilder("
");
             var stackFrame = frameInfo.StackFrame;
             var method = stackFrame.GetMethod();
+            var displayInfo = frameInfo.MethodDisplayInfo;
 
             // Special case: no method available
             if (method == null)
@@ -98,23 +98,23 @@ namespace Microsoft.AspNetCore.Hosting
                     return @"
--- exception rethrown ---
"; } - string prefix, friendlyName; - SplitTypeIntoPrefixAndFriendlyName(type, out prefix, out friendlyName); - builder.AppendFormat(CultureInfo.InvariantCulture, @"at {0}", HtmlEncodeAndReplaceLineBreaks(prefix)); - builder.Append(HtmlEncodeAndReplaceLineBreaks(friendlyName)); + var typeName = displayInfo.DeclaringTypeName; + // Separate the namespace component from the type name so that we can format it differently. + if (!string.IsNullOrEmpty(typeName) && !string.IsNullOrEmpty(type.Namespace)) + { + builder.Append($@"at {HtmlEncodeAndReplaceLineBreaks(type.Namespace)}"); + typeName = typeName.Substring(type.Namespace.Length + 1); + } + + builder.Append(HtmlEncodeAndReplaceLineBreaks(typeName)); } // Next, write the method signature - builder.Append(HtmlEncodeAndReplaceLineBreaks("." + method.Name)); - - // Is this method generic? - if (method.IsGenericMethod) - { - builder.Append(HtmlEncodeAndReplaceLineBreaks(BuildMethodGenericParametersUnescaped(method))); - } + builder.Append(displayInfo.Name); // Build method parameters - builder.AppendFormat(CultureInfo.InvariantCulture, @"{0}", HtmlEncodeAndReplaceLineBreaks(BuildMethodParametersUnescaped(method))); + var methodParameters = "(" + string.Join(", ", displayInfo.Parameters) + ")"; + builder.Append($@"{HtmlEncodeAndReplaceLineBreaks(methodParameters)}"); // Do we have source information for this frame? if (stackFrame.GetILOffset() != -1) @@ -131,21 +131,6 @@ namespace Microsoft.AspNetCore.Hosting return builder.ToString(); } - private static string BuildMethodGenericParametersUnescaped(MethodBase method) - { - Debug.Assert(method.IsGenericMethod); - return "<" + string.Join(", ", method.GetGenericArguments().Select(PrettyPrintTypeName)) + ">"; - } - - private static string BuildMethodParametersUnescaped(MethodBase method) - { - return "(" + string.Join(", ", method.GetParameters().Select(p => - { - var parameterType = p.ParameterType; - return ((parameterType != null) ? PrettyPrintTypeName(parameterType) : "?") + " " + p.Name; - })) + ")"; - } - private static string GetResourceString(string name, bool escapeBraces = false) { // '{' and '}' are special in CSS, so we use "[[[0]]]" instead for {0} (and so on). @@ -217,20 +202,6 @@ namespace Microsoft.AspNetCore.Hosting return (didReadFailingLine) ? errorSubContents : null; } - private static string PrettyPrintTypeName(Type type) => TypeNameHelper.GetTypeDisplayName(type, fullName: false); - - - private static void SplitTypeIntoPrefixAndFriendlyName(Type type, out string prefix, out string friendlyName) - { - prefix = type.Namespace; - friendlyName = PrettyPrintTypeName(type); - - if (!string.IsNullOrEmpty(friendlyName) && !string.IsNullOrEmpty(prefix)) - { - prefix += "."; - } - } - private static string GenerateFooterEncoded() { var runtimeType = HtmlEncodeAndReplaceLineBreaks(Microsoft.Extensions.Internal.RuntimeEnvironment.RuntimeType);