diff --git a/src/ApiService/ApiService/Log.cs b/src/ApiService/ApiService/Log.cs index 7a953f905..335a6dc62 100644 --- a/src/ApiService/ApiService/Log.cs +++ b/src/ApiService/ApiService/Log.cs @@ -276,3 +276,27 @@ public class LogTracerFactory : ILogTracerFactory { } } + +public interface ILogSinks { + List GetLogSinks(); +} + +public class LogSinks : ILogSinks { + private readonly List _loggers; + + public LogSinks(IServiceConfig config) { + _loggers = new List(); + foreach (var dest in config.LogDestinations) { + _loggers.Add( + dest switch { + LogDestination.AppInsights => new AppInsights(config.ApplicationInsightsInstrumentationKey!), + LogDestination.Console => new Console(), + _ => throw new Exception($"Unhandled Log Destination type: {dest}"), + } + ); + } + } + public List GetLogSinks() { + return _loggers; + } +} diff --git a/src/ApiService/ApiService/Program.cs b/src/ApiService/ApiService/Program.cs index 8f4c114e6..b04dbd745 100644 --- a/src/ApiService/ApiService/Program.cs +++ b/src/ApiService/ApiService/Program.cs @@ -36,22 +36,6 @@ public class Program { } } - - public static List GetLoggers(IServiceConfig config) { - List loggers = new List(); - foreach (var dest in config.LogDestinations) { - loggers.Add( - dest switch { - LogDestination.AppInsights => new AppInsights(config.ApplicationInsightsInstrumentationKey!), - LogDestination.Console => new Console(), - _ => throw new Exception($"Unhandled Log Destination type: {dest}"), - } - ); - } - return loggers; - } - - //Move out expensive resources into separate class, and add those as Singleton // ArmClient, Table Client(s), Queue Client(s), HttpClient, etc. public async static Async.Task Main() { @@ -72,8 +56,9 @@ public class Program { services .AddScoped(s => { + var logSinks = s.GetRequiredService(); var cfg = s.GetRequiredService(); - return new LogTracerFactory(GetLoggers(cfg)) + return new LogTracerFactory(logSinks.GetLogSinks()) .CreateLogTracer( Guid.Empty, severityLevel: cfg.LogSeverityLevel); @@ -118,6 +103,7 @@ public class Program { .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddHttpClient() .AddMemoryCache(); }