From f04d1973e7210594011cfedc70f3c06fc91f40a5 Mon Sep 17 00:00:00 2001 From: Dominion Date: Sat, 25 Mar 2023 15:48:44 -0400 Subject: [PATCH] Fix Sevice logging --- src/Tgstation.Server.Host.Service/Program.cs | 72 +++++++------------ .../ServerService.cs | 66 +++++++---------- .../TestServerService.cs | 13 ++-- 3 files changed, 58 insertions(+), 93 deletions(-) diff --git a/src/Tgstation.Server.Host.Service/Program.cs b/src/Tgstation.Server.Host.Service/Program.cs index 47eb8647dd..95afc4ca21 100644 --- a/src/Tgstation.Server.Host.Service/Program.cs +++ b/src/Tgstation.Server.Host.Service/Program.cs @@ -160,61 +160,37 @@ namespace Tgstation.Server.Host.Service } } - ServerService service = null; - ILoggerFactory loggerFactory; - try + if (Install) { - loggerFactory = LoggerFactory.Create(builder => - { - if (Configure) - { - builder.AddConsole(); - } + if (Uninstall) + return; // oh no, it's retarded... - service = new ServerService(builder, Trace ? LogLevel.Trace : Debug ? LogLevel.Debug : LogLevel.Information); - }); - } - catch - { - service?.Dispose(); - throw; - } - - using (loggerFactory) - using (service) - { - if (Install) - { - if (Uninstall) - return; // oh no, it's retarded... - - RunServiceInstall(); - - if (Configure) - { - Console.WriteLine("For this first run we'll launch the console runner so you may use the setup wizard."); - Console.WriteLine("If it starts successfully, feel free to close it and then start the service from the Windows control panel."); - } - } - else if (Uninstall) - using (var installer = new ServiceInstaller()) - { - installer.Context = new InstallContext("tgs-uninstall.log", null); - installer.ServiceName = ServerService.Name; - installer.Uninstall(null); - } - else if (!Configure) - { - service.SetupWatchdog(WatchdogFactory.CreateWatchdog(loggerFactory)); - ServiceBase.Run(service); - } + RunServiceInstall(); if (Configure) { - // DCT: None available - await WatchdogFactory.CreateWatchdog(loggerFactory).RunAsync(true, Array.Empty(), default); + Console.WriteLine("For this first run we'll launch the console runner so you may use the setup wizard."); + Console.WriteLine("If it starts successfully, feel free to close it and then start the service from the Windows control panel."); } } + else if (Uninstall) + using (var installer = new ServiceInstaller()) + { + installer.Context = new InstallContext("tgs-uninstall.log", null); + installer.ServiceName = ServerService.Name; + installer.Uninstall(null); + } + else if (!Configure) + { + using (var service = new ServerService(WatchdogFactory, Trace ? LogLevel.Trace : Debug ? LogLevel.Debug : LogLevel.Information)) + ServiceBase.Run(service); + } + + if (Configure) + { + using (var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole())) + await WatchdogFactory.CreateWatchdog(loggerFactory).RunAsync(true, Array.Empty(), default); // DCT: None available + } } } } diff --git a/src/Tgstation.Server.Host.Service/ServerService.cs b/src/Tgstation.Server.Host.Service/ServerService.cs index a2696d02df..d1adffdaf1 100644 --- a/src/Tgstation.Server.Host.Service/ServerService.cs +++ b/src/Tgstation.Server.Host.Service/ServerService.cs @@ -25,10 +25,15 @@ namespace Tgstation.Server.Host.Service /// /// The for the . /// - IWatchdog watchdog; + readonly IWatchdogFactory watchdogFactory; /// - /// The recieved from of . + /// The minimum for the . + /// + readonly LogLevel minimumLogLevel; + + /// + /// The that represents the running service. /// Task watchdogTask; @@ -40,41 +45,15 @@ namespace Tgstation.Server.Host.Service /// /// Initializes a new instance of the class. /// - /// The to configure. - /// The minimum to record in the event log. - public ServerService(ILoggingBuilder loggingBuilder, LogLevel minumumLogLevel) + /// The value of . + /// The minimum to record in the event log. + public ServerService(IWatchdogFactory watchdogFactory, LogLevel minimumLogLevel) { - if (loggingBuilder == null) - throw new ArgumentNullException(nameof(loggingBuilder)); - - loggingBuilder.AddEventLog(new EventLogSettings - { - LogName = EventLog.Log, - MachineName = EventLog.MachineName, - SourceName = EventLog.Source, - Filter = (message, logLevel) => logLevel >= minumumLogLevel, - }); - + this.watchdogFactory = watchdogFactory ?? throw new ArgumentNullException(nameof(watchdogFactory)); + this.minimumLogLevel = minimumLogLevel; ServiceName = Name; } - /// - /// Setup the for the service. - /// - /// The value of . - public void SetupWatchdog(IWatchdog watchdog) - { - if (watchdog == null) -#pragma warning disable IDE0016 // Use 'throw' expression - throw new ArgumentNullException(nameof(watchdog)); -#pragma warning restore IDE0016 // Use 'throw' expression - - if (this.watchdog != null) - throw new InvalidOperationException("SetupWatchdog called twice!"); - - this.watchdog = watchdog; - } - /// protected override void Dispose(bool disposing) { @@ -85,12 +64,20 @@ namespace Tgstation.Server.Host.Service /// protected override void OnStart(string[] args) { - if (watchdog == null) - throw new InvalidOperationException("Cannot start without watchdog!"); + var loggerFactory = LoggerFactory.Create(builder => builder.AddEventLog(new EventLogSettings + { + LogName = EventLog.Log, + MachineName = EventLog.MachineName, + SourceName = EventLog.Source, + Filter = (message, logLevel) => logLevel >= minimumLogLevel, + })); + + var watchdog = watchdogFactory.CreateWatchdog(loggerFactory); cancellationTokenSource?.Dispose(); cancellationTokenSource = new CancellationTokenSource(); - watchdogTask = RunWatchdog(args, cancellationTokenSource.Token); + + watchdogTask = RunWatchdog(watchdog, args, cancellationTokenSource.Token); } /// @@ -101,12 +88,13 @@ namespace Tgstation.Server.Host.Service } /// - /// Executes the , stopping the service if it exits. + /// Executes the , stopping the service if it exits. /// - /// The arguments for the . + /// The to run. + /// The arguments for the . /// The for the operation. /// A representing the running operation. - async Task RunWatchdog(string[] args, CancellationToken cancellationToken) + async Task RunWatchdog(IWatchdog watchdog, string[] args, CancellationToken cancellationToken) { await watchdog.RunAsync(false, args, cancellationTokenSource.Token); diff --git a/tests/Tgstation.Server.Host.Service.Tests/TestServerService.cs b/tests/Tgstation.Server.Host.Service.Tests/TestServerService.cs index a01c4b5181..03846f15a6 100644 --- a/tests/Tgstation.Server.Host.Service.Tests/TestServerService.cs +++ b/tests/Tgstation.Server.Host.Service.Tests/TestServerService.cs @@ -20,8 +20,8 @@ namespace Tgstation.Server.Host.Service.Tests public void TestConstructionAndDisposal() { Assert.ThrowsException(() => new ServerService(null, default)); - var mockLoggingBuilder = Mock.Of(); - new ServerService(mockLoggingBuilder, default).Dispose(); + var mockWatchdogFactory = new Mock(); + new ServerService(mockWatchdogFactory.Object, default).Dispose(); } [TestMethod] @@ -35,16 +35,17 @@ namespace Tgstation.Server.Host.Service.Tests var args = Array.Empty(); CancellationToken cancellationToken; mockWatchdog.Setup(x => x.RunAsync(false, args, It.IsAny())).Callback((bool x, string[] _, CancellationToken token) => cancellationToken = token).Returns(Task.CompletedTask).Verifiable(); - var mockLoggerFactory = Mock.Of(); + var mockWatchdogFactory = new Mock(); + mockWatchdogFactory.Setup(x => x.CreateWatchdog(It.IsNotNull())).Returns(mockWatchdog.Object).Verifiable(); - using (var service = new ServerService(mockLoggerFactory, default)) + using (var service = new ServerService(mockWatchdogFactory.Object, default)) { - Assert.ThrowsException(() => onStart.Invoke(service, new object[] { args })); - service.SetupWatchdog(mockWatchdog.Object); onStart.Invoke(service, new object[] { args }); onStop.Invoke(service, Array.Empty()); mockWatchdog.VerifyAll(); } + + mockWatchdogFactory.VerifyAll(); } } }