// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Diagnostics
{
    /// <summary>Provides a set of static methods for working with Exceptions.</summary>
    internal static class ExceptionHelpers
    {
        public static TException InitializeStackTrace<TException>(this TException e) where TException : Exception
        {
            Debug.Assert(e != null);
            Debug.Assert(e.StackTrace == null);

            try { throw e; }
            catch { return e; }
        }
    }
}

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Diagnostics
{
    /// <summary>Provides a set of static methods for working with Exceptions.</summary>
    internal static class ExceptionHelpers
    {
        public static TException InitializeStackTrace<TException>(this TException e) where TException : Exception
        {
            Debug.Assert(e != null);

            // Ideally we'd be able to populate e.StackTrace with the current stack trace.
            // We could throw the exception and catch it, but the populated StackTrace would
            // not extend beyond this frame.  Instead, we grab a stack trace and store it into
            // the exception's data dictionary, at least making the info available for debugging,
            // albeit not part of the string returned by e.ToString() or e.StackTrace.
            e.Data["StackTrace"] = Environment.StackTrace;

            return e;
        }
    }
}

Change InitializeStackTrace helper to store trace in Data
Change InitializeStackTrace helper to store trace in Data

My previous attempt to get a better stack trace in exceptions was insufficient.  I was throwing/catching the exception to initialize the StackTrace, but I neglected the fact that the stack trace is filled in as the exception propagates, and so throwing/catching in the same frame means that the stack trace just stores that one frame, which isn't helpful.  Instead, this changes the helper to store the stack trace obtained via Environment.StackTrace into the Exception's Data dictionary.  This means the exception doesn't show up in a e.ToString() or e.StackTrace rendering, but it's at least stored for debugging purposes.
