Flush logs when crashing.

When the process is running, we use a finally block to call Log.Dispose and flush any outstanding logs to disk before the process exits. This works when we handle any exception in a matching catch block.

When the exception is unhandled, then the finally block will not run and instead the process will just exit. To fix this, flush the logs inside a catch block instead before rethrowing the error. This ensures we get logs even when crashing.
This commit is contained in:
RoosterDragon
2024-08-03 11:18:46 +01:00
committed by Gustas
parent 8a56e14d7a
commit 323204014c
4 changed files with 39 additions and 9 deletions

View File

@@ -44,6 +44,13 @@ namespace OpenRA
{
Run(args);
}
catch
{
// Flush logs before rethrowing, i.e. allowing the exception to go unhandled.
// try-finally won't work - an unhandled exception kills our process without running the finally block!
Log.Dispose();
throw;
}
finally
{
Log.Dispose();
@@ -133,6 +140,7 @@ namespace OpenRA
if (e is NoSuchCommandException)
{
Console.WriteLine(e.Message);
Log.Dispose(); // Flush logs before we terminate the process.
Environment.Exit(1);
}
else