From ec4c0b87e76a573d70d3a1c560995d8073b49f6f Mon Sep 17 00:00:00 2001 From: Mohammad Moattar Date: Tue, 30 Jul 2024 17:06:12 +0100 Subject: [PATCH] Fix the serilog bug --- GitVersion.yml | 2 +- .../ApiPipelineOptions.cs | 19 +++++------- .../ApplicationBuilderExtensions.cs | 30 ++++++++++++------- src/LittleBlocks.AspNetCore/HostAsWeb.cs | 2 +- .../Logging/SeriLog/Fluent/LoggerBuilder.cs | 25 +++++++--------- .../Core/CorrelateRequestMiddleware.cs | 4 +-- .../ImpersonationAuthenticationHandler.cs | 2 +- .../HostAsWindowsService.cs | 5 ++-- .../SinkBuilderExtensions.cs | 4 +-- .../CorrelateRequestMiddlewareTests.cs | 2 +- .../HealthControllerTests.cs | 2 +- .../Helpers/StartupForAuthentication.cs | 6 ++-- .../Helpers/StartupForAutomapper.cs | 15 ++++------ .../Helpers/StartupForConfiguration.cs | 15 ++++------ .../Helpers/StartupForHealth.cs | 7 +++-- .../Helpers/StartupForIntegration.cs | 7 +++-- .../Helpers/StartupForValues.cs | 6 ++-- .../Helpers/TestApplicationFactory.cs | 18 ++++------- .../LittleBlocks.Sample.WebAPI/Startup.cs | 4 +-- .../Startup.cs | 6 ++-- 20 files changed, 89 insertions(+), 92 deletions(-) diff --git a/GitVersion.yml b/GitVersion.yml index d554f5e..2431649 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -1,4 +1,4 @@ -next-version: 2.1.0 +next-version: 2.2.0 mode: Mainline major-version-bump-message: '\+semver:\s?(breaking|major)' minor-version-bump-message: '\+semver:\s?(feature|minor)' diff --git a/src/LittleBlocks.AspNetCore.Bootstrap/ApiPipelineOptions.cs b/src/LittleBlocks.AspNetCore.Bootstrap/ApiPipelineOptions.cs index 3e2f005..aeefc31 100644 --- a/src/LittleBlocks.AspNetCore.Bootstrap/ApiPipelineOptions.cs +++ b/src/LittleBlocks.AspNetCore.Bootstrap/ApiPipelineOptions.cs @@ -16,19 +16,14 @@ namespace LittleBlocks.AspNetCore.Bootstrap; -public sealed class ApiPipelineOptions +public sealed class ApiPipelineOptions( + IConfiguration configuration, + IHostEnvironment environment, + ILoggerFactory loggerFactory) { - public ApiPipelineOptions(IConfiguration configuration, IHostEnvironment environment, - ILoggerFactory loggerFactory) - { - Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); - Environment = environment ?? throw new ArgumentNullException(nameof(environment)); - LoggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); - } - - public IConfiguration Configuration { get; } - public IHostEnvironment Environment { get; } - public ILoggerFactory LoggerFactory { get; } + public IConfiguration Configuration { get; } = configuration ?? throw new ArgumentNullException(nameof(configuration)); + public IHostEnvironment Environment { get; } = environment ?? throw new ArgumentNullException(nameof(environment)); + public ILoggerFactory LoggerFactory { get; } = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); public Action PostAuthenticationConfigure { get; } = null; public Action PreEndPointsConfigure { get; } = null; public Action PostEndPointsConfigure { get; } = null; diff --git a/src/LittleBlocks.AspNetCore.Bootstrap/ApplicationBuilderExtensions.cs b/src/LittleBlocks.AspNetCore.Bootstrap/ApplicationBuilderExtensions.cs index 6de716c..006f65e 100644 --- a/src/LittleBlocks.AspNetCore.Bootstrap/ApplicationBuilderExtensions.cs +++ b/src/LittleBlocks.AspNetCore.Bootstrap/ApplicationBuilderExtensions.cs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using Serilog; + namespace LittleBlocks.AspNetCore.Bootstrap; public static class ApplicationBuilderExtensions @@ -21,21 +23,29 @@ public static class ApplicationBuilderExtensions public static void UseDefaultApiPipeline(this IApplicationBuilder app, IConfiguration configuration, IWebHostEnvironment env, + IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - if (app == null) throw new ArgumentNullException(nameof(app)); - if (configuration == null) throw new ArgumentNullException(nameof(configuration)); - if (env == null) throw new ArgumentNullException(nameof(env)); - if (loggerFactory == null) throw new ArgumentNullException(nameof(loggerFactory)); + ArgumentNullException.ThrowIfNull(lifetime); + ArgumentNullException.ThrowIfNull(app); + ArgumentNullException.ThrowIfNull(configuration); + ArgumentNullException.ThrowIfNull(env); + ArgumentNullException.ThrowIfNull(loggerFactory); + InitiateFlushOutstandingOperations(lifetime); var options = new ApiPipelineOptions(configuration, env, loggerFactory); app.UseDefaultApiPipeline(options); } - public static void UseDefaultApiPipeline(this IApplicationBuilder app, ApiPipelineOptions options) + private static void InitiateFlushOutstandingOperations(IHostApplicationLifetime lifetime) + { + lifetime.ApplicationStopped.Register(Log.CloseAndFlush); + } + + private static void UseDefaultApiPipeline(this IApplicationBuilder app, ApiPipelineOptions options) { - if (app == null) throw new ArgumentNullException(nameof(app)); - if (options == null) throw new ArgumentNullException(nameof(options)); + ArgumentNullException.ThrowIfNull(app); + ArgumentNullException.ThrowIfNull(options); var appInfo = options.Configuration.GetApplicationInfo(); var authOptions = options.Configuration.GetAuthOptions(); @@ -87,8 +97,8 @@ public static void UseDefaultApiPipeline(this IApplicationBuilder app, ApiPipeli public static void UseStartPage(this IEndpointRouteBuilder endpoints, string applicationName) { - if (endpoints == null) throw new ArgumentNullException(nameof(endpoints)); - if (applicationName == null) throw new ArgumentNullException(nameof(applicationName)); + ArgumentNullException.ThrowIfNull(endpoints); + ArgumentNullException.ThrowIfNull(applicationName); endpoints.MapGet("/", context => { @@ -128,6 +138,6 @@ private static string LoadStartPageFromEmbeddedResource(string applicationName) private static void LogResolvedEnvironment(IHostEnvironment env, ILoggerFactory loggerFactory) { var log = loggerFactory.CreateLogger("Startup"); - log.LogInformation($"Application is started in '{env.EnvironmentName.ToUpper()}' environment ..."); + log.LogInformation($"{nameof(Application)} is started in '{env.EnvironmentName.ToUpper()}' environment ..."); } } diff --git a/src/LittleBlocks.AspNetCore/HostAsWeb.cs b/src/LittleBlocks.AspNetCore/HostAsWeb.cs index 5bf1b7f..658c26e 100644 --- a/src/LittleBlocks.AspNetCore/HostAsWeb.cs +++ b/src/LittleBlocks.AspNetCore/HostAsWeb.cs @@ -39,7 +39,7 @@ private static IWebHost Build( }) .ConfigureServices((context, services) => { - loggerConfigure(new LoggerBuilder(context.HostingEnvironment, context.Configuration, new LoggerConfiguration())).Build(); + loggerConfigure(new LoggerBuilder(services, context.HostingEnvironment, context.Configuration)).Build(); }) .Build(); diff --git a/src/LittleBlocks.AspNetCore/Logging/SeriLog/Fluent/LoggerBuilder.cs b/src/LittleBlocks.AspNetCore/Logging/SeriLog/Fluent/LoggerBuilder.cs index 7aed15e..fa5e11d 100644 --- a/src/LittleBlocks.AspNetCore/Logging/SeriLog/Fluent/LoggerBuilder.cs +++ b/src/LittleBlocks.AspNetCore/Logging/SeriLog/Fluent/LoggerBuilder.cs @@ -16,7 +16,11 @@ namespace LittleBlocks.AspNetCore.Logging.SeriLog.Fluent; -public sealed class LoggerBuilder : ILoggerBuilder, IBuildLogger +public sealed class LoggerBuilder( + IServiceCollection services, + IHostEnvironment environment, + IConfiguration configuration) + : ILoggerBuilder, IBuildLogger { private const string LogMessageTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{MachineName}] [{EnvironmentUserName}] [{ProcessId}] " + @@ -25,25 +29,18 @@ public sealed class LoggerBuilder : ILoggerBuilder, IBuildLogger private const string SerilogConfigSectionName = "Logging:Serilog"; private const string SerilogMinimumLevelKey = "MinimumLevel"; private const LogEventLevel DefaultLogLevel = LogEventLevel.Information; - private readonly IHostEnvironment _environment; - private readonly IConfiguration _configuration; - private readonly LoggerConfiguration _loggerConfiguration; + + private readonly IServiceCollection _services = services ?? throw new ArgumentNullException(nameof(services)); + private readonly IHostEnvironment _environment = environment ?? throw new ArgumentNullException(nameof(environment)); + private readonly IConfiguration _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); private Func _optionsProvider; private Func _sinksProvider; - public LoggerBuilder(IHostEnvironment environment, IConfiguration configuration, LoggerConfiguration loggerConfiguration) - { - _environment = environment ?? throw new ArgumentNullException(nameof(environment)); - _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); - _loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration)); - } - public void Build() where TStartup : class { - ConfigureLogger(_loggerConfiguration, _optionsProvider, _sinksProvider); + _services.AddSerilog(lg => ConfigureLogger(lg, _optionsProvider, _sinksProvider)); } - public IConfiguration Configuration => _configuration; public IHostEnvironment Environment => _environment; @@ -86,7 +83,7 @@ private void ConfigureLogger(LoggerConfiguration loggerConfiguration, Func optionsProvider, Func sinksProvider) where TStartup : class { - if (loggerConfiguration == null) throw new ArgumentNullException(nameof(loggerConfiguration)); + ArgumentNullException.ThrowIfNull(loggerConfiguration); var env = _environment; var configuration = _configuration; diff --git a/src/LittleBlocks.AspNetCore/RequestCorrelation/Core/CorrelateRequestMiddleware.cs b/src/LittleBlocks.AspNetCore/RequestCorrelation/Core/CorrelateRequestMiddleware.cs index 08178ca..e6770ba 100644 --- a/src/LittleBlocks.AspNetCore/RequestCorrelation/Core/CorrelateRequestMiddleware.cs +++ b/src/LittleBlocks.AspNetCore/RequestCorrelation/Core/CorrelateRequestMiddleware.cs @@ -54,12 +54,12 @@ private HttpContext CorrelateRequest(HttpContext context) new List {HttpHeaders.HttpCorrelationId, HttpHeaders.HttpRequestId}); correlationId = _correlationIdProvider.GenerateId(); - context.Request.Headers.Add(HttpHeaders.HttpRequestId, correlationId); + context.Request.Headers.Append(HttpHeaders.HttpRequestId, correlationId); } context.Response.OnStarting(state => { - ((HttpContext) state).Response.Headers.Add(HttpHeaders.HttpRequestId, correlationId); + ((HttpContext) state).Response.Headers.Append(HttpHeaders.HttpRequestId, correlationId); return Task.FromResult(0); }, context); diff --git a/src/LittleBlocks.AspNetCore/Security/Impersonation/ImpersonationAuthenticationHandler.cs b/src/LittleBlocks.AspNetCore/Security/Impersonation/ImpersonationAuthenticationHandler.cs index 225ddb3..66927c2 100644 --- a/src/LittleBlocks.AspNetCore/Security/Impersonation/ImpersonationAuthenticationHandler.cs +++ b/src/LittleBlocks.AspNetCore/Security/Impersonation/ImpersonationAuthenticationHandler.cs @@ -23,7 +23,7 @@ namespace LittleBlocks.AspNetCore.Security.Impersonation; public sealed class ImpersonationAuthenticationHandler : AuthenticationHandler { public ImpersonationAuthenticationHandler(IOptionsMonitor options, - ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock) + ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock): base(options, logger, encoder, clock) { } diff --git a/src/LittleBlocks.Hosting.WindowsService/HostAsWindowsService.cs b/src/LittleBlocks.Hosting.WindowsService/HostAsWindowsService.cs index f17de06..a75c7f6 100644 --- a/src/LittleBlocks.Hosting.WindowsService/HostAsWindowsService.cs +++ b/src/LittleBlocks.Hosting.WindowsService/HostAsWindowsService.cs @@ -41,10 +41,9 @@ private static IHost Build( config.ConfigureBuilder(options); configurationConfigure(config); }) - .UseSerilog((context, configuration) => + .ConfigureServices((context, services) => { - loggerConfigure(new LoggerBuilder(context.HostingEnvironment, context.Configuration, - configuration)).Build(); + loggerConfigure(new LoggerBuilder(services, context.HostingEnvironment, context.Configuration)).Build(); }) .Build(); diff --git a/src/LittleBlocks.Logging.SeriLog/SinkBuilderExtensions.cs b/src/LittleBlocks.Logging.SeriLog/SinkBuilderExtensions.cs index 239110a..2cd8b14 100644 --- a/src/LittleBlocks.Logging.SeriLog/SinkBuilderExtensions.cs +++ b/src/LittleBlocks.Logging.SeriLog/SinkBuilderExtensions.cs @@ -20,8 +20,8 @@ public static class SinkBuilderExtensions { public static ISinkBuilderContext BuildAndCloneContext(this ISinkBuilder builder, ISinkBuilderContext sinkBuilderContext) { - if (builder == null) throw new ArgumentNullException(nameof(builder)); - if (sinkBuilderContext == null) throw new ArgumentNullException(nameof(sinkBuilderContext)); + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(sinkBuilderContext); var loggerConfiguration = builder.Build(); return sinkBuilderContext.Clone(loggerConfiguration); diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/CorrelateRequestMiddlewareTests.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/CorrelateRequestMiddlewareTests.cs index 300b396..4f58c99 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/CorrelateRequestMiddlewareTests.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/CorrelateRequestMiddlewareTests.cs @@ -40,7 +40,7 @@ public async Task Given_CorrelationId_IsPresent_ThenInvokeNextMiddlewareComponen var next = Substitute.For(); var sut = new CorrelateRequestMiddleware(next, options, idProvider); var context = GetDefaultHttpContext(); - context.Request.Headers.Add(header, Guid.NewGuid().ToString()); + context.Request.Headers.Append(header, Guid.NewGuid().ToString()); // Act await sut.Invoke(context); diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/HealthControllerTests.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/HealthControllerTests.cs index 6ac8916..206f487 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/HealthControllerTests.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/HealthControllerTests.cs @@ -25,7 +25,7 @@ public sealed class HealthControllerTests public async Task GivenAPIRunning_WhenHealthRequested_ShouldReturnHealthy() { // Arrange - using var fixture = TestApplicationFactory.Create(); + await using var fixture = TestApplicationFactory.Create(); // Act var response = await fixture.CreateClient().GetAsync($"health"); diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForAuthentication.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForAuthentication.cs index 8cc7e17..459652f 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForAuthentication.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForAuthentication.cs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using Microsoft.Extensions.Hosting; + namespace LittleBlocks.Sample.WebAPI.IntegrationTests.Helpers; public abstract class StartupForAuthentication where T : class @@ -45,8 +47,8 @@ public void ConfigureServices(IServiceCollection services) ); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - app.UseDefaultApiPipeline(Configuration, env, loggerFactory); + app.UseDefaultApiPipeline(Configuration, env, lifetime, loggerFactory); } } diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForAutomapper.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForAutomapper.cs index 325904d..c41aa07 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForAutomapper.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForAutomapper.cs @@ -14,16 +14,13 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using Microsoft.Extensions.Hosting; + namespace LittleBlocks.Sample.WebAPI.IntegrationTests.Helpers; -public class StartupForAutomapper +public class StartupForAutomapper(IConfiguration configuration) { - public StartupForAutomapper(IConfiguration configuration) - { - Configuration = configuration; - } - - private IConfiguration Configuration { get; } + private IConfiguration Configuration { get; } = configuration; public void ConfigureServices(IServiceCollection services) { @@ -44,8 +41,8 @@ public void ConfigureServices(IServiceCollection services) ); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - app.UseDefaultApiPipeline(Configuration, env, loggerFactory); + app.UseDefaultApiPipeline(Configuration, env, lifetime, loggerFactory); } } diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForConfiguration.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForConfiguration.cs index a4de217..7543fca 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForConfiguration.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForConfiguration.cs @@ -14,16 +14,13 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using Microsoft.Extensions.Hosting; + namespace LittleBlocks.Sample.WebAPI.IntegrationTests.Helpers; -public class StartupForConfiguration +public class StartupForConfiguration(IConfiguration configuration) { - public StartupForConfiguration(IConfiguration configuration) - { - Configuration = configuration; - } - - private IConfiguration Configuration { get; } + private IConfiguration Configuration { get; } = configuration; public void ConfigureServices(IServiceCollection services) { @@ -35,8 +32,8 @@ public void ConfigureServices(IServiceCollection services) ); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - app.UseDefaultApiPipeline(Configuration, env, loggerFactory); + app.UseDefaultApiPipeline(Configuration, env, lifetime, loggerFactory); } } diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForHealth.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForHealth.cs index 84d657a..d5edab1 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForHealth.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForHealth.cs @@ -15,6 +15,7 @@ // along with this program. If not, see . using System.Net.Http; +using Microsoft.Extensions.Hosting; namespace LittleBlocks.Sample.WebAPI.IntegrationTests.Helpers; @@ -46,10 +47,10 @@ public void ConfigureServices(IServiceCollection services) }) ); } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - app.UseDefaultApiPipeline(Configuration, env, loggerFactory); + app.UseDefaultApiPipeline(Configuration, env, lifetime, loggerFactory); } } diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForIntegration.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForIntegration.cs index c2906f1..8c99f90 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForIntegration.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForIntegration.cs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using Microsoft.Extensions.Hosting; + namespace LittleBlocks.Sample.WebAPI.IntegrationTests.Helpers; public class StartupForIntegration @@ -42,9 +44,8 @@ public void ConfigureServices(IServiceCollection services) }) ); } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - app.UseDefaultApiPipeline(Configuration, env, loggerFactory); + app.UseDefaultApiPipeline(Configuration, env, lifetime, loggerFactory); } } diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForValues.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForValues.cs index a5a1dda..f25da3b 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForValues.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/StartupForValues.cs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using Microsoft.Extensions.Hosting; + namespace LittleBlocks.Sample.WebAPI.IntegrationTests.Helpers; public class StartupForValues @@ -43,8 +45,8 @@ public void ConfigureServices(IServiceCollection services) ); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - app.UseDefaultApiPipeline(Configuration, env, loggerFactory); + app.UseDefaultApiPipeline(Configuration, env, lifetime, loggerFactory); } } diff --git a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/TestApplicationFactory.cs b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/TestApplicationFactory.cs index 468832d..73b295f 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/TestApplicationFactory.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI.IntegrationTests/Helpers/TestApplicationFactory.cs @@ -19,20 +19,14 @@ namespace LittleBlocks.Sample.WebAPI.IntegrationTests.Helpers; -public sealed class TestApplicationFactory : WebApplicationFactory where TStartup : class +public sealed class TestApplicationFactory(TestApplicationOptions options) : WebApplicationFactory + where TStartup : class { private const string LogsDirectoryPattern = "Logs\\{0}"; - private const int LoggerFlushDelayInMs = 1000; - private readonly string _logFilePattern = - $"LittleBlocks.Sample.WebAPI.IntegrationTests-{EnvironmentNames.Integration}" + "-{0}.log"; + private const string LogFilePattern = $"LittleBlocks.Sample.WebAPI.IntegrationTests-{EnvironmentNames.Integration}" + "-{0}.log"; - private readonly TestApplicationOptions _options; - - public TestApplicationFactory(TestApplicationOptions options) - { - _options = options ?? throw new ArgumentNullException(nameof(options)); - } + private readonly TestApplicationOptions _options = options ?? throw new ArgumentNullException(nameof(options)); private Guid SessionId { get; } = Guid.NewGuid(); @@ -41,7 +35,7 @@ public string LogFilePath get { var logsDirectory = LogDirectoryPath; - var fileName = string.Format(_logFilePattern, DateTime.Today.ToString("yyyyMMdd")); + var fileName = string.Format(LogFilePattern, DateTime.Today.ToString("yyyyMMdd")); return Path.Combine(logsDirectory, fileName); } } @@ -65,7 +59,7 @@ protected override IWebHostBuilder CreateWebHostBuilder() env.EnvironmentName = _options.Environment; var configOptions = new ConfigurationOptions(env.ContentRootPath, env.EnvironmentName, - env.ApplicationName, new string[] { }); + env.ApplicationName, []); builder.ConfigureBuilder(configOptions); }) .UseStartup() diff --git a/src/Samples/LittleBlocks.Sample.WebAPI/Startup.cs b/src/Samples/LittleBlocks.Sample.WebAPI/Startup.cs index 60d7e7d..0f2d173 100644 --- a/src/Samples/LittleBlocks.Sample.WebAPI/Startup.cs +++ b/src/Samples/LittleBlocks.Sample.WebAPI/Startup.cs @@ -65,9 +65,9 @@ public void ConfigureServices(IServiceCollection services) ); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - app.UseDefaultApiPipeline(Configuration, env, loggerFactory); + app.UseDefaultApiPipeline(Configuration, env, lifetime, loggerFactory); app.UseEndpoints(config => { config.MapHealthChecksUI(m => m.AddCustomStylesheet("health-ui.css")); diff --git a/src/Samples/LittleBlocks.Sample.WindowsService/Startup.cs b/src/Samples/LittleBlocks.Sample.WindowsService/Startup.cs index d13805d..d581cec 100644 --- a/src/Samples/LittleBlocks.Sample.WindowsService/Startup.cs +++ b/src/Samples/LittleBlocks.Sample.WindowsService/Startup.cs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using Microsoft.Extensions.Hosting; + namespace LittleBlocks.Sample.WindowsService; public class Startup @@ -38,8 +40,8 @@ public void ConfigureServices(IServiceCollection services) ); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, ILoggerFactory loggerFactory) { - app.UseDefaultApiPipeline(Configuration, env, loggerFactory); + app.UseDefaultApiPipeline(Configuration, env, lifetime, loggerFactory); } }