fix(server): report Windows service start and stop failures to the event log

A service has no console, and a refused data folder or server.json throws
before the server's log file exists, so the SCM recorded only error 1064
and the entry to check was lost. ServiceMain and the stop path now also
write the reason to the Application event log (source TitanoServer,
advapi32 ReportEventW, no message file registered). The Windows README
says where to look and that a data folder created by an unelevated
console run must be deleted or given to Administrators first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 15:51:22 +00:00
1 parent 32a0e9bbc1
commit 1dc93932cc
6 files changed
+134 -6

No files matched your search

@@ -9,7 +9,8 @@ namespace Titano.Server.Hosting.Windows;
/// <summary>
/// Runs the server as a Windows service with the SCM API directly (StartServiceCtrlDispatcherW,
/// RegisterServiceCtrlHandlerExW, SetServiceStatus). Stop and shutdown requests stop the web host gracefully: running
/// jobs keep their checkpoints and resume at the next start.
/// jobs keep their checkpoints and resume at the next start. A failure to start or stop goes to the Application event
/// log (<see cref="ServiceEventLog"/>), since a service has no console.
/// </summary>
[SupportedOSPlatform("windows")]
public static class WindowsServiceHost
@@ -56,7 +57,9 @@ public static class WindowsServiceHost
}
catch (Exception ex)
{
// A refused data folder or server.json says which entry to check; the SCM itself records only error 1064.
Console.Error.WriteLine(ex);
ServiceEventLog.Error(ServiceEventLog.StartFailed, ServiceEventLog.Describe("Titano Server did not start:", ex));
Report(Status.Stopped(ErrorExceptionInService));
}
}
@@ -96,6 +99,7 @@ public static class WindowsServiceHost
catch (Exception ex)
{
Console.Error.WriteLine(ex);
ServiceEventLog.Error(ServiceEventLog.StopFailed, ServiceEventLog.Describe("Titano Server did not stop cleanly:", ex));
exitCode = ErrorExceptionInService;
}
Report(Status.Stopped(exitCode));