mirror of
https://github.com/microsoft/onefuzz.git
synced 2025-06-23 06:38: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
|
//Move out expensive resources into separate class, and add those as Singleton
|
||||||
// ArmClient, Table Client(s), Queue Client(s), HttpClient, etc.
|
// ArmClient, Table Client(s), Queue Client(s), HttpClient, etc.
|
||||||
public async static Async.Task Main() {
|
public async static Async.Task Main() {
|
||||||
@ -72,8 +56,9 @@ public class Program {
|
|||||||
|
|
||||||
services
|
services
|
||||||
.AddScoped<ILogTracer>(s => {
|
.AddScoped<ILogTracer>(s => {
|
||||||
|
var logSinks = s.GetRequiredService<ILogSinks>();
|
||||||
var cfg = s.GetRequiredService<IServiceConfig>();
|
var cfg = s.GetRequiredService<IServiceConfig>();
|
||||||
return new LogTracerFactory(GetLoggers(cfg))
|
return new LogTracerFactory(logSinks.GetLogSinks())
|
||||||
.CreateLogTracer(
|
.CreateLogTracer(
|
||||||
Guid.Empty,
|
Guid.Empty,
|
||||||
severityLevel: cfg.LogSeverityLevel);
|
severityLevel: cfg.LogSeverityLevel);
|
||||||
@ -118,6 +103,7 @@ public class Program {
|
|||||||
.AddSingleton<ICreds, Creds>()
|
.AddSingleton<ICreds, Creds>()
|
||||||
.AddSingleton<IServiceConfig, ServiceConfiguration>()
|
.AddSingleton<IServiceConfig, ServiceConfiguration>()
|
||||||
.AddSingleton<IStorage, Storage>()
|
.AddSingleton<IStorage, Storage>()
|
||||||
|
.AddSingleton<ILogSinks, LogSinks>()
|
||||||
.AddHttpClient()
|
.AddHttpClient()
|
||||||
.AddMemoryCache();
|
.AddMemoryCache();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user