Reputation: 184
Using .Net 5 WPF, Serilog and PrismLibrary 8 with Unity Can someone please guide me in the right direction so I can inject Microsoft.Extensions.Logging.ILogger into ViewModels etc. I have found many snippets, but all fails on some point or another. I prefer using Microsoft.Extensions.Logging.ILogger for flexibility in coice of logging provider.
Upvotes: 2
Views: 472
Reputation: 11
the way I solved this in Prism 8 using DryIoc is to add a reference to Serilog.Extensions.Logging
You can then create a microsoft logger that your viewmodels could use with the SerilogLoggerFactory. So make your logger, than pass it to the factory and then register the output of the factory in the containerRegistry.
var microsoftLogger = new SerilogLoggerFactory(Log.Logger).CreateLogger("Main");
containerRegistry.Register<Microsoft.Extensions.Logging.ILogger>(() => microsoftLogger);
Upvotes: 0
Reputation: 1
I was also looking for some sort of example too. But I believe most of them use the ILoggerFacade and they since removed it for Prism 8. At the moment I'm using MAUI and it could be similar for WPF. I followed this video https://www.youtube.com/watch?v=WicmnH72kf0 but I did some changes for it to work with Prism.
You're gonna want to add nuget extension: Prism.Container.Extensions
Then add the following code (which is based off of Gerald's video) in your PrismStartup or wherever you have your RegisterTypes:
containerRegistry.RegisterServices(services =>
{
services.AddLogging(loggingBuilder => loggingBuilder.AddTraceLogger(_ => { }));
});
Wherever you want to log add:
using Microsoft.Extensions.Logging;
public class IWantToLogHereClass
{
private readonly ILogger<IWantToLogHereClass> _logger;
public IWantToLogHereClass(ILogger<IWantToLogHereClass> logger)
{
_logger = logger;
}
//...
//whatever place you want to log
_logger.LogInformation("HELLO WORLD!");
//...
}
This did it for me, hope this helps!
Upvotes: 0