AppDomain.FirstChanceException and stack overflow exception

前端 未结 7 1818

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

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


        
7条回答
  •  忘掉有多难
    2021-02-05 11:25

    This is working for me:

    private volatile bool _insideFirstChanceExceptionHandler;    
    
    // ...
    
    AppDomain.CurrentDomain.FirstChanceException += OnFirstChanceException;
    
    // ...
    
    private void OnFirstChanceException(object sender, FirstChanceExceptionEventArgs args)
    {
        if (_insideFirstChanceExceptionHandler)
        {
            // Prevent recursion if an exception is thrown inside this method
            return;
        }
    
        _insideFirstChanceExceptionHandler = true;
        try
        {
            // Code which may throw an exception
        }
        catch
        {
            // You have to catch all exceptions inside this method
        }
        finally
        {
            _insideFirstChanceExceptionHandler = false;
        }
    }
    

提交回复
热议问题