From a3e87cfe6d20f9ba7788bda9a3c33d1a86c97110 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Fri, 23 Feb 2018 22:12:35 -0800 Subject: [PATCH] Address more stacktrace print issues (#16525) * Fix newline issue/duplicate message on Contract failures * make it work on Unix as well * cleanup * Add a newline between error/user messages and stacktrace --- src/mscorlib/shared/System/Diagnostics/Debug.Unix.cs | 14 +++++++++++++- src/mscorlib/shared/System/Diagnostics/Debug.cs | 14 ++++++++++++-- .../src/System/Diagnostics/Contracts/ContractsBCL.cs | 8 ++++---- src/mscorlib/src/System/Diagnostics/Debug.Windows.cs | 14 +++++++++++++- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/mscorlib/shared/System/Diagnostics/Debug.Unix.cs b/src/mscorlib/shared/System/Diagnostics/Debug.Unix.cs index 0554581..627ea4a 100644 --- a/src/mscorlib/shared/System/Diagnostics/Debug.Unix.cs +++ b/src/mscorlib/shared/System/Diagnostics/Debug.Unix.cs @@ -21,7 +21,19 @@ namespace System.Diagnostics // In Core, we do not show a dialog. // Fail in order to avoid anyone catching an exception and masking // an assert failure. - var ex = new DebugAssertException(message, detailMessage, stackTrace); + DebugAssertException ex; + if (message == String.Empty) + { + ex = new DebugAssertException(stackTrace); + } + else if (detailMessage == String.Empty) + { + ex = new DebugAssertException(message, stackTrace); + } + else + { + ex = new DebugAssertException(message, detailMessage, stackTrace); + } Environment.FailFast(ex.Message, ex, errorSource); } } diff --git a/src/mscorlib/shared/System/Diagnostics/Debug.cs b/src/mscorlib/shared/System/Diagnostics/Debug.cs index 3a29738..7bc43cc 100644 --- a/src/mscorlib/shared/System/Diagnostics/Debug.cs +++ b/src/mscorlib/shared/System/Diagnostics/Debug.cs @@ -113,7 +113,7 @@ namespace System.Diagnostics string stackTrace; try { - stackTrace = new StackTrace(0, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal); + stackTrace = new StackTrace(2, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal); } catch { @@ -326,8 +326,18 @@ namespace System.Diagnostics private sealed class DebugAssertException : Exception { + internal DebugAssertException(string stackTrace) : + base(Environment.NewLine + stackTrace) + { + } + + internal DebugAssertException(string message, string stackTrace) : + base(message + Environment.NewLine + Environment.NewLine + stackTrace) + { + } + internal DebugAssertException(string message, string detailMessage, string stackTrace) : - base(message + Environment.NewLine + detailMessage + Environment.NewLine + stackTrace) + base(message + Environment.NewLine + detailMessage + Environment.NewLine + Environment.NewLine + stackTrace) { } } diff --git a/src/mscorlib/src/System/Diagnostics/Contracts/ContractsBCL.cs b/src/mscorlib/src/System/Diagnostics/Contracts/ContractsBCL.cs index 7b74b01..790620c 100644 --- a/src/mscorlib/src/System/Diagnostics/Contracts/ContractsBCL.cs +++ b/src/mscorlib/src/System/Diagnostics/Contracts/ContractsBCL.cs @@ -359,22 +359,22 @@ namespace System.Runtime.CompilerServices private static String GetDisplayMessage(ContractFailureKind failureKind, String userMessage, String conditionText) { - String resourceName = GetResourceNameForFailure(failureKind); + String failureMessage; // Well-formatted English messages will take one of four forms. A sentence ending in // either a period or a colon, the condition string, then the message tacked // on to the end with two spaces in front. // Note that both the conditionText and userMessage may be null. Also, // on Silverlight we may not be able to look up a friendly string for the - // error message. Let's leverage Silverlight's default error message there. - String failureMessage; + // error message. Let's leverage Silverlight's default error message there. if (!String.IsNullOrEmpty(conditionText)) { + String resourceName = GetResourceNameForFailure(failureKind); resourceName += "_Cnd"; failureMessage = SR.Format(SR.GetResourceString(resourceName), conditionText); } else { - failureMessage = SR.GetResourceString(resourceName); + failureMessage = ""; } // Now add in the user message, if present. diff --git a/src/mscorlib/src/System/Diagnostics/Debug.Windows.cs b/src/mscorlib/src/System/Diagnostics/Debug.Windows.cs index e1992b4..7649991 100644 --- a/src/mscorlib/src/System/Diagnostics/Debug.Windows.cs +++ b/src/mscorlib/src/System/Diagnostics/Debug.Windows.cs @@ -17,7 +17,19 @@ namespace System.Diagnostics // In Core, we do not show a dialog. // Fail in order to avoid anyone catching an exception and masking // an assert failure. - var ex = new DebugAssertException(message, detailMessage, stackTrace); + DebugAssertException ex; + if (message == String.Empty) + { + ex = new DebugAssertException(stackTrace); + } + else if (detailMessage == String.Empty) + { + ex = new DebugAssertException(message, stackTrace); + } + else + { + ex = new DebugAssertException(message, detailMessage, stackTrace); + } Environment.FailFast(ex.Message, ex, errorSource); } } -- 2.7.4