Reputation: 26355
I am writing a C# .NET Core 5.0 console application. This application uses CommandLineUtils to process command line arguments, Unity Container for DI, and Serilog for logging.
I am registering Serilog in my composition root:
public static void Setup(IUnityContainer container)
{
container.RegisterFactory<ILogger>(_ => new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger(),
new SingletonLifetimeManager());
}
However, I have a command line argument, --debug
, that should reveal DEBUG level logs. If this option is not specified, it should keep the default INFO level. From the Serilog examples, the way they set DEBUG level is by adding calls to LoggerConfiguration
object (i.e. MinimumLevel.Debug()
)
But I don't know if I need to call this until after CLI processing takes place, which happens after I define the composition root.
Seems like I'm in a catch 22 situation. I know it's bad practice to RegisterType()
outside of the root of the application. How should I solve this circular dependency?
EDIT
Here is my Program
class which shows the order of things:
internal static class Program
{
private static IUnityContainer Container { get; } = new UnityContainer();
private static void Main(string[] args)
{
CompositionRoot.Setup(Container);
var app = new CommandLineApplication<TrashCommand>();
app.Conventions
.UseDefaultConventions()
.UseConstructorInjection(new UnityServiceProvider(Container));
app.Execute(args);
Log.CloseAndFlush();
}
}
Upvotes: 1
Views: 547
Reputation: 172646
Perhaps I misinterpreted your question, but doesn't a construct such as the following answer your question?
public static void Setup(IUnityContainer container, bool logDebug)
{
LogEventLevel level = logDebug ? LogEventLevel.Debug : LogEventLevel.Info.
container.RegisterFactory<ILogger>(_ => new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Is(level)
.CreateLogger(),
new SingletonLifetimeManager());
}
Or, alternatively:
public static void Setup(IUnityContainer container, bool logDebug)
{
LogEventLevel level = logDebug ? LogEventLevel.Debug : LogEventLevel.Info.
container.RegisterFactory<ILogger>(_ =>
{
var config = new LoggerConfiguration().WriteTo.Console();
if (logDebug) config = config.MinimumLevel.Is(level);
return config.CreateLogger();
},
new SingletonLifetimeManager());
}
Upvotes: 1