AppDomain.FirstChanceException and stack overflow exception

前端 未结 7 1814

I\'m using the FirstChanceException event to log details about any thrown exceptions.

static void Main(string[] args)
{
    AppDomain.CurrentDomain.FirstChanceEx         


        
7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 11:16

    First use a method instead of a delegate, so the method name will be defined

    Then use Environment.StackTrace to check if the method is already in the stacktrace

    Here is a piece of code untested:

    static void Main(string[] args) 
    { 
        AppDomain.CurrentDomain.FirstChanceException += handleFirstChanceException;
    } 
    
    private void handleFirstChanceException(object sender, EventArgs eventArgs)
    {
        if (Environment.StackTrace.Contains("handleFirstChanceException"))
            return;
    
        // handle
    }
    

    I think the above will not work because it'll always contains the method name, but you can count if it appear more than 1 time. Also, check that it's not inlined when you compile in Release mode, in this case you're in trouble

提交回复
热议问题