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

Commit

Permalink
Make log sinks a singleton (#2247)
Browse files Browse the repository at this point in the history
* Make log sinks a singleton, continue passing new log tracer for every invocation

* Remove no longer used code
  • Loading branch information
tevoinea authored Aug 15, 2022
1 parent dcb3096 commit ef0367c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
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

0 comments on commit ef0367c

Please sign in to comment.