只需要在 App.xaml.cs 中添加以下代码:
protected override void RegisterTypes(IContainerRegistry containerRegistry) { var factory = new NLog.Extensions.Logging.NLogLoggerFactory(); ILogger logger = factory.CreateLogger("NLog"); containerRegistry.RegisterInstance(logger); }
就可以在 XXViewModel.cs 的构造函数里添加 ILogger logger 入参并使用:
public XXViewModel(Microsoft.Extensions.Logging.ILogger logger) { logger.LogDebug("ViewModel 里的日志"); }
代码片段来自: https://www.bilibili.com/video/BV1KQ4y1C7tg?p=6
标签:containerRegistry,C#,ILogger,cs,Logger,IoC,logger From: https://www.cnblogs.com/z5337/p/17927109.html