From 49bfa5e6651bcc68b7282ff7af41be3456cf98aa Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Tue, 3 Nov 2020 12:33:54 -0500 Subject: [PATCH] Add a small amount of logging to ServerFactory --- src/Tgstation.Server.Host/ServerFactory.cs | 14 ++++++++------ .../Setup/IPostSetupServices{TLoggerType}.cs | 16 ++++++++++++++++ .../Setup/PostSetupServices.cs | 16 +++++++++++++--- .../Setup/SetupApplication.cs | 4 ++-- 4 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 src/Tgstation.Server.Host/Setup/IPostSetupServices{TLoggerType}.cs diff --git a/src/Tgstation.Server.Host/ServerFactory.cs b/src/Tgstation.Server.Host/ServerFactory.cs index 2008459b9a..3a27b0eecc 100644 --- a/src/Tgstation.Server.Host/ServerFactory.cs +++ b/src/Tgstation.Server.Host/ServerFactory.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using System; using System.Threading; using System.Threading.Tasks; @@ -51,16 +52,17 @@ namespace Tgstation.Server.Host var setupWizardHostBuilder = CreateDefaultBuilder() .UseSetupApplication(); - IPostSetupServices postSetupServices; + IPostSetupServices postSetupServices; using (var setupHost = setupWizardHostBuilder.Build()) { - postSetupServices = setupHost.Services.GetRequiredService(); + postSetupServices = setupHost.Services.GetRequiredService>(); await setupHost.RunAsync(cancellationToken).ConfigureAwait(false); - } - if (postSetupServices.GeneralConfiguration.SetupWizardMode == SetupWizardMode.Only) - { - return null; + if (postSetupServices.GeneralConfiguration.SetupWizardMode == SetupWizardMode.Only) + { + postSetupServices.Logger.LogInformation("Shutting down due to only running setup wizard."); + return null; + } } var hostBuilder = CreateDefaultBuilder() diff --git a/src/Tgstation.Server.Host/Setup/IPostSetupServices{TLoggerType}.cs b/src/Tgstation.Server.Host/Setup/IPostSetupServices{TLoggerType}.cs new file mode 100644 index 0000000000..029ec74c55 --- /dev/null +++ b/src/Tgstation.Server.Host/Setup/IPostSetupServices{TLoggerType}.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Logging; + +namespace Tgstation.Server.Host.Setup +{ + /// + /// with a . + /// + /// The category for . + interface IPostSetupServices : IPostSetupServices + { + /// + /// The . + /// + ILogger Logger { get; } + } +} diff --git a/src/Tgstation.Server.Host/Setup/PostSetupServices.cs b/src/Tgstation.Server.Host/Setup/PostSetupServices.cs index 48353fbb5b..f595e3ba65 100644 --- a/src/Tgstation.Server.Host/Setup/PostSetupServices.cs +++ b/src/Tgstation.Server.Host/Setup/PostSetupServices.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Options; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.System; @@ -6,7 +7,7 @@ using Tgstation.Server.Host.System; namespace Tgstation.Server.Host.Setup { /// - sealed class PostSetupServices : IPostSetupServices + sealed class PostSetupServices : IPostSetupServices { /// public IPlatformIdentifier PlatformIdentifier { get; } @@ -20,6 +21,9 @@ namespace Tgstation.Server.Host.Setup /// public FileLoggingConfiguration FileLoggingConfiguration => fileLoggingConfigurationOptions.Value; + /// + public ILogger Logger { get; } + /// /// Backing for . /// @@ -36,19 +40,25 @@ namespace Tgstation.Server.Host.Setup readonly IOptions fileLoggingConfigurationOptions; /// - /// Initializes a new instance of the . + /// Initializes a new instance of the . /// /// The value of . + /// The used to create . /// The containing the value of . /// The containing the value of . /// The containing the value of . public PostSetupServices( IPlatformIdentifier platformIdentifier, + ILoggerFactory loggerFactory, IOptions generalConfigurationOptions, IOptions databaseConfigurationOptions, IOptions fileLoggingConfigurationOptions) { PlatformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier)); + if (loggerFactory == null) + throw new ArgumentNullException(nameof(loggerFactory)); + + Logger = loggerFactory.CreateLogger(); this.generalConfigurationOptions = generalConfigurationOptions ?? throw new ArgumentNullException(nameof(generalConfigurationOptions)); this.databaseConfigurationOptions = databaseConfigurationOptions ?? throw new ArgumentNullException(nameof(databaseConfigurationOptions)); this.fileLoggingConfigurationOptions = fileLoggingConfigurationOptions ?? throw new ArgumentNullException(nameof(fileLoggingConfigurationOptions)); diff --git a/src/Tgstation.Server.Host/Setup/SetupApplication.cs b/src/Tgstation.Server.Host/Setup/SetupApplication.cs index 87548b1145..9f848efa8f 100644 --- a/src/Tgstation.Server.Host/Setup/SetupApplication.cs +++ b/src/Tgstation.Server.Host/Setup/SetupApplication.cs @@ -1,4 +1,4 @@ -using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog.Events; @@ -73,7 +73,7 @@ namespace Tgstation.Server.Host.Setup /// The to configure. protected virtual void ConfigureHostedService(IServiceCollection services) { - services.AddSingleton(); + services.AddSingleton(typeof(IPostSetupServices<>), typeof(PostSetupServices<>)); services.AddSingleton(); } }