mirror of
https://github.com/microsoft/onefuzz.git
synced 2025-06-22 22:28:50 +00:00
Make log sinks a singleton (#2247)
* Make log sinks a singleton, continue passing new log tracer for every invocation * Remove no longer used code
This commit is contained in:
@ -276,3 +276,27 @@ public class LogTracerFactory : ILogTracerFactory {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -36,22 +36,6 @@ public class Program {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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() {
|
||||
@ -72,8 +56,9 @@ public class Program {
|
||||
|
||||
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);
|
||||
@ -118,6 +103,7 @@ public class Program {
|
||||
.AddSingleton<ICreds, Creds>()
|
||||
.AddSingleton<IServiceConfig, ServiceConfiguration>()
|
||||
.AddSingleton<IStorage, Storage>()
|
||||
.AddSingleton<ILogSinks, LogSinks>()
|
||||
.AddHttpClient()
|
||||
.AddMemoryCache();
|
||||
}
|
||||
|
Reference in New Issue
Block a user