Skip to content

Commit 82a84ef

Browse files
authored
Added unhandled exceptions handler on server (SubnauticaNitrox#1288)
* Added unhandled exceptions handler on server * Disabled asking for input if Console.In is unavailable * Reverted some code formatting * Removed try catch around server start
1 parent 5d2686c commit 82a84ef

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

NitroxModel/Logger/Log.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.IO;
45
using System.Reflection;
56
using NLog;
67
using NLog.Conditions;
@@ -31,6 +32,8 @@ public static class Log
3132
private static NLog.Logger logger;
3233

3334
public static InGameLogger InGameLogger { private get; set; }
35+
36+
public static string FileName { get; private set; }
3437

3538
public static void Setup(bool performanceCritical = false)
3639
{
@@ -66,9 +69,10 @@ public static void Setup(bool performanceCritical = false)
6669
ForegroundColor = ConsoleOutputColor.DarkGray
6770
});
6871

72+
FileName = Path.GetFullPath($"Nitrox Logs/Nitrox-{GetLoggerName()}.log");
6973
FileTarget logFile = new FileTarget(nameof(logFile))
7074
{
71-
FileName = $"Nitrox Logs/Nitrox-{GetLoggerName()}.log",
75+
FileName = FileName,
7276
ArchiveFileName = $"Nitrox Logs/archives/Nitrox-{GetLoggerName()}.{{#}}.log",
7377
ArchiveEvery = FileArchivePeriod.Day,
7478
ArchiveNumbering = ArchiveNumberingMode.Date,

NitroxServer-Subnautica/ConsoleWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NitroxServer_Subnautica
77
/// <summary>
88
/// Helper methods to manage the console window used by this process.
99
/// </summary>
10-
public class ConsoleWindow
10+
public static class ConsoleWindow
1111
{
1212
/// <summary>
1313
/// This flag enables the user to use the mouse to select and edit text. To enable

NitroxServer-Subnautica/Program.cs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Globalization;
45
using System.IO;
56
using System.Reflection;
@@ -45,6 +46,7 @@ private static void Main(string[] args)
4546
});
4647
}
4748

49+
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
4850
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
4951
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += CurrentDomainOnAssemblyResolve;
5052

@@ -53,22 +55,10 @@ private static void Main(string[] args)
5355
NitroxServiceLocator.InitializeDependencyContainer(new SubnauticaServerAutoFacRegistrar());
5456
NitroxServiceLocator.BeginNewLifetimeScope();
5557

56-
Server server;
57-
try
58+
Server server = NitroxServiceLocator.LocateService<Server>();
59+
if (!server.Start())
5860
{
59-
server = NitroxServiceLocator.LocateService<Server>();
60-
61-
if (!server.Start())
62-
{
63-
Log.Error("Unable to start server.");
64-
Console.WriteLine("\nPress any key to continue..");
65-
Console.ReadKey(true);
66-
}
67-
}
68-
catch (Exception ex)
69-
{
70-
Log.Error(ex);
71-
return;
61+
throw new Exception("Unable to start server.");
7262
}
7363

7464
CatchExitEvent();
@@ -80,6 +70,34 @@ private static void Main(string[] args)
8070
}
8171
}
8272

73+
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
74+
{
75+
if (e.ExceptionObject is Exception ex)
76+
{
77+
Log.Error(ex);
78+
}
79+
if (!Environment.UserInteractive || Console.In == StreamReader.Null)
80+
{
81+
return;
82+
}
83+
84+
Console.WriteLine("Press L to open log file before closing. Press any other key to close . . .");
85+
ConsoleKeyInfo key = Console.ReadKey(true);
86+
if (key.Key == ConsoleKey.L)
87+
{
88+
Log.Info($"Opening log file at: {Log.FileName}..");
89+
string fileOpenerProgram = Environment.OSVersion.Platform switch
90+
{
91+
PlatformID.MacOSX => "open",
92+
PlatformID.Unix => "xdg-open",
93+
_ => "explorer"
94+
};
95+
Process.Start(fileOpenerProgram, Log.FileName);
96+
}
97+
98+
Environment.Exit(1);
99+
}
100+
83101
private static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
84102
{
85103
string dllFileName = args.Name.Split(',')[0];
@@ -160,7 +178,7 @@ private static void CatchExitEvent()
160178
[DllImport("kernel32.dll", SetLastError = true)]
161179
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
162180

163-
// Prevents Garbage Collection issue where server closes and an exception occurs for this handle.
181+
// Prevents Garbage Collection freeing this callback's memory. Causing an exception to occur for this handle.
164182
private static readonly ConsoleEventDelegate consoleCtrlCheckDelegate = ConsoleEventCallback;
165183

166184
private static bool ConsoleEventCallback(int eventType)

0 commit comments

Comments
 (0)