How do I record stacktrace of StackOverflowException?

I've read answers to similar questions about handling StackOverflowException, and I understand that starting from .net 2.0, the StackOverflowException terminates the process, and I'm fine with this.

However, when stack overflow happens, I want to know why it happened, specifically I want to record the stacktrace.

In Microsoft documentation, it's stated that you can process stackoverflow exception if you set proper attributes to the method:

Starting with the .NET Framework 4, this event is not raised for exceptions that corrupt the state of the process, such as stack overflows or access violations, unless the event handler is security-critical and has the HandleProcessCorruptedStateExceptionsAttribute attribute.

So I've tried setting HandleProcessCorruptedStateExceptionsAttribute to my handler method:

class Program
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        Main(null);
    }

    [SecurityCritical]
    [HandleProcessCorruptedStateExceptionsAttribute]
    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Console.WriteLine(e.ExceptionObject);
    }
}

The program still terminates without giving me the stacktrace.

How can I record a stacktrace for StackOverflowException?

链接地址: http://www.djcxy.com/p/80764.html

上一篇: ShimsContext.Create抛出StackOverflowException

下一篇: 如何记录StackOverflowException的堆栈跟踪?