Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Make log sinks a singleton #2247

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/ApiService/ApiService/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,27 @@ public LogTracer CreateLogTracer(Guid correlationId, IEnumerable<(string, string
}

}

public interface ILogSinks {
List<ILog> GetLogSinks();
}

public class LogSinks : ILogSinks {
private readonly List<ILog> _loggers;

public LogSinks(IServiceConfig config) {
_loggers = new List<ILog>();
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<ILog> GetLogSinks() {
return _loggers;
}
}
20 changes: 3 additions & 17 deletions src/ApiService/ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,6 @@ public async Async.Task Invoke(FunctionContext context, FunctionExecutionDelegat
}
}


public static List<ILog> GetLoggers(IServiceConfig config) {
List<ILog> loggers = new List<ILog>();
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() {
Expand All @@ -72,8 +56,9 @@ public async static Async.Task Main() {

services
.AddScoped<ILogTracer>(s => {
var logSinks = s.GetRequiredService<ILogSinks>();
var cfg = s.GetRequiredService<IServiceConfig>();
return new LogTracerFactory(GetLoggers(cfg))
return new LogTracerFactory(logSinks.GetLogSinks())
.CreateLogTracer(
Guid.Empty,
severityLevel: cfg.LogSeverityLevel);
Expand Down Expand Up @@ -118,6 +103,7 @@ public async static Async.Task Main() {
.AddSingleton<ICreds, Creds>()
.AddSingleton<IServiceConfig, ServiceConfiguration>()
.AddSingleton<IStorage, Storage>()
.AddSingleton<ILogSinks, LogSinks>()
.AddHttpClient()
.AddMemoryCache();
}
Expand Down