Skip to content

Commit

Permalink
VCST-2785: load Serilog configuration after module initialize (#2893)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Feb 25, 2025
1 parent b7606e8 commit 6edbd97
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/VirtoCommerce.Platform.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,10 @@ public Startup(IConfiguration configuration, IWebHostEnvironment hostingEnvironm
public void ConfigureServices(IServiceCollection services)
{
// Use temporary bootstrap logger (which will be replaced with configured version later) until DI initialization completed
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateBootstrapLogger();

services.AddSerilog((serviceProvider, loggerConfiguration) =>
{
_ = loggerConfiguration.ReadFrom.Configuration(Configuration);

// Enrich configuration from external sources
var configurationServices = serviceProvider.GetService<IEnumerable<ILoggerConfigurationService>>();
foreach (var service in configurationServices)
{
service.Configure(loggerConfiguration);
}
// Preserve static logger (i.e. create new logger for DI, instead of reconfiguring existing)
// to avoid exception about frozen logger because BuildServiceProvider is called multiple times
}, preserveStaticLogger: true);
var loggerConfiguration = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.Debug();
Log.Logger = loggerConfiguration.CreateBootstrapLogger();

Log.ForContext<Startup>().Information("Virto Commerce is loading");

Expand Down Expand Up @@ -513,7 +502,22 @@ public void ConfigureServices(IServiceCollection services)
services.AddOptions<ExternalModuleCatalogOptions>().Bind(Configuration.GetSection("ExternalModules")).ValidateDataAnnotations();
services.AddExternalModules();

//HangFire
// Serilog (initialize after all modules DLLs were loaded)
services.AddSerilog((serviceProvider, loggerConfiguration) =>
{
_ = loggerConfiguration.ReadFrom.Configuration(Configuration);

// Enrich configuration from external sources
var configurationServices = serviceProvider.GetService<IEnumerable<ILoggerConfigurationService>>();
foreach (var service in configurationServices)
{
service.Configure(loggerConfiguration);
}
// Preserve static logger (i.e. create new logger for DI, instead of reconfiguring existing)
// to avoid exception about frozen logger because BuildServiceProvider is called multiple times
}, preserveStaticLogger: true);

// HangFire
services.AddHangfire(Configuration);

// Register the Swagger generator
Expand Down

0 comments on commit 6edbd97

Please sign in to comment.