@@ -58,6 +58,13 @@ public class FileSystemRuntimeConfigLoader : RuntimeConfigLoader
5858 /// </summary>
5959 private readonly IFileSystem _fileSystem ;
6060
61+ /// <summary>
62+ /// Logger used to log all the events that occur inside of FileSystemRuntimeConfigLoader
63+ /// </summary>
64+ private ILogger < FileSystemRuntimeConfigLoader > ? _logger ;
65+
66+ private StartupLogBuffer ? _logBuffer ;
67+
6168 public const string CONFIGFILE_NAME = "dab-config" ;
6269 public const string CONFIG_EXTENSION = ".json" ;
6370 public const string ENVIRONMENT_PREFIX = "DAB_" ;
@@ -81,13 +88,17 @@ public FileSystemRuntimeConfigLoader(
8188 HotReloadEventHandler < HotReloadEventArgs > ? handler = null ,
8289 string baseConfigFilePath = DEFAULT_CONFIG_FILE_NAME ,
8390 string ? connectionString = null ,
84- bool isCliLoader = false )
91+ bool isCliLoader = false ,
92+ ILogger < FileSystemRuntimeConfigLoader > ? logger = null ,
93+ StartupLogBuffer ? logBuffer = null )
8594 : base ( handler , connectionString )
8695 {
8796 _fileSystem = fileSystem ;
8897 _baseConfigFilePath = baseConfigFilePath ;
8998 ConfigFilePath = GetFinalConfigFilePath ( ) ;
9099 _isCliLoader = isCliLoader ;
100+ _logger = logger ;
101+ _logBuffer = logBuffer ;
91102 }
92103
93104 /// <summary>
@@ -195,7 +206,7 @@ public bool TryLoadConfig(
195206 {
196207 if ( _fileSystem . File . Exists ( path ) )
197208 {
198- Console . WriteLine ( $ "Loading config file from { _fileSystem . Path . GetFullPath ( path ) } .") ;
209+ SendLogToBufferOrLogger ( LogLevel . Information , $ "Loading config file from { _fileSystem . Path . GetFullPath ( path ) } .") ;
199210
200211 // Use File.ReadAllText because DAB doesn't need write access to the file
201212 // and ensures the file handle is released immediately after reading.
@@ -214,7 +225,8 @@ public bool TryLoadConfig(
214225 }
215226 catch ( IOException ex )
216227 {
217- Console . WriteLine ( $ "IO Exception, retrying due to { ex . Message } ") ;
228+ SendLogToBufferOrLogger ( LogLevel . Warning , $ "IO Exception, retrying due to { ex . Message } ") ;
229+
218230 if ( runCount == FileUtilities . RunLimit )
219231 {
220232 throw ;
@@ -237,8 +249,7 @@ public bool TryLoadConfig(
237249 {
238250 if ( TrySetupConfigFileWatcher ( ) )
239251 {
240- Console . WriteLine ( "Monitoring config: {0} for hot-reloading." , ConfigFilePath ) ;
241- logger ? . LogInformation ( "Monitoring config: {ConfigFilePath} for hot-reloading." , ConfigFilePath ) ;
252+ SendLogToBufferOrLogger ( LogLevel . Information , $ "Monitoring config: { ConfigFilePath } for hot-reloading.") ;
242253 }
243254
244255 // When isDevMode is not null it means we are in a hot-reload scenario, and need to save the previous
@@ -248,14 +259,7 @@ public bool TryLoadConfig(
248259 // Log error when the mode is changed during hot-reload.
249260 if ( isDevMode != this . RuntimeConfig . IsDevelopmentMode ( ) )
250261 {
251- if ( logger is null )
252- {
253- Console . WriteLine ( "Hot-reload doesn't support switching mode. Please restart the service to switch the mode." ) ;
254- }
255- else
256- {
257- logger . LogError ( "Hot-reload doesn't support switching mode. Please restart the service to switch the mode." ) ;
258- }
262+ SendLogToBufferOrLogger ( LogLevel . Error , "Hot-reload doesn't support switching mode. Please restart the service to switch the mode." ) ;
259263 }
260264
261265 RuntimeConfig . Runtime . Host . Mode = ( bool ) isDevMode ? HostMode . Development : HostMode . Production ;
@@ -280,16 +284,8 @@ public bool TryLoadConfig(
280284 return false ;
281285 }
282286
283- if ( logger is null )
284- {
285- string errorMessage = $ "Unable to find config file: { path } does not exist.";
286- Console . Error . WriteLine ( errorMessage ) ;
287- }
288- else
289- {
290- string errorMessage = "Unable to find config file: {path} does not exist." ;
291- logger . LogError ( message : errorMessage , path ) ;
292- }
287+ string errorMessage = $ "Unable to find config file: { path } does not exist.";
288+ SendLogToBufferOrLogger ( LogLevel . Error , errorMessage ) ;
293289
294290 config = null ;
295291 return false ;
@@ -515,4 +511,35 @@ public void UpdateConfigFilePath(string filePath)
515511 _baseConfigFilePath = filePath ;
516512 ConfigFilePath = filePath ;
517513 }
514+
515+ public void SetLogger ( ILogger < FileSystemRuntimeConfigLoader > logger )
516+ {
517+ _logger = logger ;
518+ }
519+
520+ /// <summary>
521+ /// Flush all logs from the buffer after the log level is set from the RuntimeConfig.
522+ /// </summary>
523+ public void FlushLogBuffer ( )
524+ {
525+ _logBuffer ? . FlushToLogger ( _logger ) ;
526+ }
527+
528+ /// <summary>
529+ /// Helper method that sends the log to the buffer if the logger has not being set up.
530+ /// Else, it will send the log to the logger.
531+ /// </summary>
532+ /// <param name="logLevel">LogLevel of the log.</param>
533+ /// <param name="message">Message that will be printed in the log.</param>
534+ private void SendLogToBufferOrLogger ( LogLevel logLevel , string message )
535+ {
536+ if ( _logger is null )
537+ {
538+ _logBuffer ? . BufferLog ( logLevel , message ) ;
539+ }
540+ else
541+ {
542+ _logger ? . Log ( logLevel , message ) ;
543+ }
544+ }
518545}
0 commit comments