Skip to content
This repository has been archived by the owner on Dec 26, 2020. It is now read-only.

Commit

Permalink
Trap and log unhandled exceptions thrown anywhere in the app
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceanswave committed Sep 7, 2020
1 parent bfa49b5 commit 5e7df8e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ void MenuExit_Click(object sender, EventArgs e)
[STAThread]
static void Main(string[] args)
{
// Associate with all unhandled exceptions
AppDomain.CurrentDomain.UnhandledException += GlobalExceptionHandler;

string appGuid =
((GuidAttribute)Assembly.GetExecutingAssembly().
GetCustomAttributes(typeof(GuidAttribute), false).
Expand Down Expand Up @@ -113,5 +116,18 @@ static void Main(string[] args)
_logger.LogError($"FSMosquitoClient shut down unexpectedly: {ex.Message}", ex);
}
}

static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
if (_logger != null)
{
_logger.LogError($"An unhandled exception occurred: {e.ExceptionObject}", e);
}

Console.WriteLine(e.ExceptionObject.ToString());
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
Environment.Exit(1);
}
}
}

0 comments on commit 5e7df8e

Please sign in to comment.