Skip to content

Instantly share code, notes, and snippets.

@Krilliac
Last active October 27, 2020 07:55
Show Gist options
  • Save Krilliac/75c93d0933121608a04883074dadbccc to your computer and use it in GitHub Desktop.
Save Krilliac/75c93d0933121608a04883074dadbccc to your computer and use it in GitHub Desktop.
private static async Task PutTaskDelay()
{
await Task.Delay(8_000); //8 seconds, split by a digit separator
}
private async void GenericExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
try
{
var ex = (Exception)e.ExceptionObject;
Log.WriteLine(LogType.CRITICAL, ex + Environment.NewLine);
Log.WriteLine(LogType.FAILED,
"Unexpected error has occured. An 'AppName-Error-yyyy-mmm-d-h-mm.log' file has been created. Check your log folder for more information.");
await using TextWriter tw = new StreamWriter(new FileStream($"AppName-Error-{Strings.Format(DateTime.Now, "yyyy-MMM-d-H-mm")}.log", FileMode.Create));
await tw.WriteAsync(ex.ToString());
tw.Close();
await tw.DisposeAsync();
await PutTaskDelay(); //Hold, but allow console to remain active for time defined in PutTaskDelay()
{
Environment.ExitCode = -1; //Finish up any remaining resources & threads, then stop
}
}
catch (Exception)
{
Environment.Exit(-1); //Hard stop, close out no matter what
}
}
@Krilliac
Copy link
Author

Krilliac commented Oct 27, 2020

LogType is a custom logger, for my project. Console.WriteLine or other alternatives may need to be implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment