unhandledexception C#

我有这段代码,我正在使用一个计时器来调用我的RunTest()函数.如果我不使用计时器,则会捕获RunTest()中的所有未处理的异常,但是使用此计时器代码它们不会被捕获.我犯了什么错误?

private static TelemetryClient telemetryClient = new TelemetryClient();

private static System.Timers.Timer aTimer;

    static void Main(string[] args)
    {

        // Register Unhandled Exception Handler
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        // Set the Instrumentation Key to track the WebJob Exception
        TelemetryConfiguration.Active.InstrumentationKey = SecureConfigReader.GetSetting("ApplicationInsightsKey");


        Configure();

        if (args.Length > 0)
        {
            // Create a timer with a ten second interval.
            aTimer = new System.Timers.Timer(10000);
            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            double Scheduled_Interval = Convert.ToDouble(args[0]);
            // Set the Interval to 300 seconds.
            aTimer.Interval = TimeSpan.FromSeconds(Scheduled_Interval).TotalMilliseconds;
            aTimer.Enabled = true;

            Console.WriteLine("Press the Enter key to exit the program.");
            Console.ReadLine();
        }
    }


    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
        RunTests();

    }
    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        ExceptionTelemetry excTelemetry = new ExceptionTelemetry((Exception)e.ExceptionObject);
        excTelemetry.SeverityLevel = SeverityLevel.Critical;
        excTelemetry.HandledAt = ExceptionHandledAt.Unhandled;
        telemetryClient.InstrumentationKey = SecureConfigReader.GetSetting("ApplicationInsightsKey");
         telemetryClient.TrackException(excTelemetry);
        //make sure all telemetry is sent before closing the app
        telemetryClient.Flush();
    }

    /*
     * 1. Find all tests
     * 2. Run all tests, collecting all results
     * 3. Process alerts if any
     * 4. Save results
     */
    private static void RunTests()
    {

        List<SyntheticTransactionTableEntity> results = new List<SyntheticTransactionTableEntity>();
        List<ISyntheticTransaction> tests = new List<ISyntheticTransaction>();
     }

最佳答案 它记录了:

The Timer component catches and suppresses all exceptions thrown by
event handlers for the Elapsed event.

您需要捕获OnTimedEvent方法,然后将该异常传播回去,例如,如果您希望CurrentDomain_UnhandledException捕获它,则将其抛出到另一个线程上(抛入ThreadPool.QueueUserWorkItem())

或者创建一个处理程序

private static void TimerExceptionHandler(Exception ex)
{
    Console.WriteLine(ex.ToString());
    ...
}

将它传递给ElapsedEventHandler:

aTimer.Elapsed += (sender, e) => { OnTimedEvent(sender, e, TimerExceptionHandler); };

抛出异常时调用它:

private static void OnTimedEvent(object source, ElapsedEventArgs e, Action<Exception> timerExceptionHandler)
    try
    {
        RunTests();
    }
    catch (Exception ex)
    {
        timerExceptionHandler(ex);
    }
点赞