I'm trying to get Serilog to be injected into classes and projects within my .NET Core 3.1 WPF application.
I'm using the Caliburn.Micro framework to handle the MVVM side of things, the Serilog Logger has been configured in the Bootstrapper class, what I'm not clear on is how I register the instance of the logger so that it can be used for DI across my app.
This is what I have in the Bootstrapper class, I've added a simple try-catch in the constructor to prove that the logger is configured.
public class Bootstrapper : BootstrapperBase
{
private SimpleContainer _container = new SimpleContainer();
public Bootstrapper()
{
Initialize();
try
{
Log.Information("In Bootstrapper Configure method");
}
catch (Exception ex)
{
Log.Fatal(ex, "The application failed to start correctly");
}
finally
{
Log.CloseAndFlush();
}
}
protected override void Configure()
{
_container.Instance(_container);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("log.txt")
.CreateLogger();
_container
.Singleton<IWindowManager, WindowManager>()
.Singleton<IEventAggregator, EventAggregator>()
_container
.PerRequest<IMyDataHandler, MyDataHandler>();
GetType().Assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => type.Name.EndsWith("ViewModel"))
.ToList()
.ForEach(viewModelType => _container.RegisterPerRequest(
viewModelType, viewModelType.ToString(), viewModelType));
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
}
protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
Any help, or pointers to documentation would be great.