Strip internal frame in Environment.StackTrace
authorJan Kotas <jkotas@microsoft.com>
Sat, 11 Mar 2017 05:39:25 +0000 (21:39 -0800)
committerJan Kotas <jkotas@microsoft.com>
Sat, 11 Mar 2017 05:59:01 +0000 (21:59 -0800)
Contributes to dotnet/coreclr#6209

Commit migrated from https://github.com/dotnet/coreclr/commit/7e50e48a24c12e19d0a0f52ac1b3d74e5d1dac77

src/coreclr/src/mscorlib/src/Internal/Runtime/Augments/EnvironmentAugments.cs
src/coreclr/src/mscorlib/src/System/Environment.cs

index b22310e..b5a56b8 100644 (file)
@@ -4,6 +4,8 @@
 
 using System;
 using System.Collections;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
 
 namespace Internal.Runtime.Augments
 {
@@ -16,7 +18,6 @@ namespace Internal.Runtime.Augments
         public static void FailFast(string message, Exception error) => Environment.FailFast(message, error);
         public static string[] GetCommandLineArgs() => Environment.GetCommandLineArgs();
         public static bool HasShutdownStarted => Environment.HasShutdownStarted;
-        public static string StackTrace => Environment.StackTrace;
         public static int TickCount => Environment.TickCount;
         public static string GetEnvironmentVariable(string variable) => Environment.GetEnvironmentVariable(variable);
         public static string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target) => Environment.GetEnvironmentVariable(variable, target);
@@ -24,5 +25,14 @@ namespace Internal.Runtime.Augments
         public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target) => Environment.GetEnvironmentVariables(target);
         public static void SetEnvironmentVariable(string variable, string value) => Environment.SetEnvironmentVariable(variable, value);
         public static void SetEnvironmentVariable(string variable, string value, EnvironmentVariableTarget target) => Environment.SetEnvironmentVariable(variable, value, target);
+
+        public static string StackTrace
+        {
+            [MethodImpl(MethodImplOptions.NoInlining)] // Prevent inlining from affecting where the stacktrace starts
+            get
+            {
+                return new StackTrace(1 /* skip this one frame */, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal);
+            }
+        }
     }
 }
index 4f1971c..9234dcc 100644 (file)
@@ -576,11 +576,11 @@ namespace System
         ==============================================================================*/
         public static String StackTrace
         {
+            [MethodImpl(MethodImplOptions.NoInlining)] // Prevent inlining from affecting where the stacktrace starts
             get
             {
                 Contract.Ensures(Contract.Result<String>() != null);
-
-                return GetStackTrace(null, true);
+                return global::Internal.Runtime.Augments.EnvironmentAugments.StackTrace;
             }
         }