From 5266046fe740a36a03d3f156677054152a125717 Mon Sep 17 00:00:00 2001 From: Teo Voinea Date: Fri, 12 Aug 2022 15:11:34 +0000 Subject: [PATCH 1/2] Make log sinks a singleton, continue passing new log tracer for every invocation --- src/ApiService/ApiService/Log.cs | 24 ++++++++++++++++++++++++ src/ApiService/ApiService/Program.cs | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/ApiService/ApiService/Log.cs b/src/ApiService/ApiService/Log.cs index 7a953f905c..335a6dc623 100644 --- a/src/ApiService/ApiService/Log.cs +++ b/src/ApiService/ApiService/Log.cs @@ -276,3 +276,27 @@ public LogTracer CreateLogTracer(Guid correlationId, IEnumerable<(string, string } } + +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 8f4c114e6b..bce24f534d 100644 --- a/src/ApiService/ApiService/Program.cs +++ b/src/ApiService/ApiService/Program.cs @@ -72,8 +72,9 @@ public async static Async.Task Main() { 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 +119,7 @@ public async static Async.Task Main() { .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddHttpClient() .AddMemoryCache(); } From 1fe595038b1d38d03ff886d04345e73bf51fd13d Mon Sep 17 00:00:00 2001 From: Teo Voinea Date: Fri, 12 Aug 2022 19:03:57 +0000 Subject: [PATCH 2/2] Remove no longer used code --- src/ApiService/ApiService/Program.cs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/ApiService/ApiService/Program.cs b/src/ApiService/ApiService/Program.cs index bce24f534d..b04dbd7458 100644 --- a/src/ApiService/ApiService/Program.cs +++ b/src/ApiService/ApiService/Program.cs @@ -36,22 +36,6 @@ public async Async.Task Invoke(FunctionContext context, FunctionExecutionDelegat } } - - 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() {