MvvmCross 7 如何在 WPF (.net Core 3.1) 应用程序中设置 Serilog [英] MvvmCross 7 How to setup Serilog in WPF (.net Core 3.1) application
问题描述
我在这里的第一篇文章,希望我会问一个有价值的问题,如果回答可能会帮助我和其他人理解 MvvmCross 的复杂性.
My first post here, hopefully I will ask a merit question that if answered maybe will help me and the others to understand the complexities of MvvmCross.
我有一个问题要问熟悉 MvvmCross 框架的人.我正在使用 MvvmCross 7.1.2 版本,并且正在尝试在我的 WPF(.net Core 3.1)应用程序中设置 Serilog 日志记录.
I have a question to people familiar with MvvmCross framework. I'm using MvvmCross 7.1.2 version and I'm trying to setup Serilog logging in my WPF (.net Core 3.1) application.
MvvmCross 的典型项目结构:Project.Core .net 标准 2.0;Project.Wpf .net Core 3.1
Project structure is typical for MvvmCross: Project.Core .net Stardard 2.0; Project.Wpf .net Core 3.1
我在 Project.Wpf 项目中创建了 Setup.cs 类,该类继承自 MvxWpfSetup 并根据 MvvmCross 文档覆盖了两个方法 GetDefaultProviderType() 和 CreateLogProvider():
I created Setup.cs class in Project.Wpf project which inherits from MvxWpfSetup and overrode two methods GetDefaultProviderType() and CreateLogProvider() as per MvvmCross documentation:
public class Setup : MvxWpfSetup
{
public override MvxLogProviderType GetDefaultLogProviderType() => MvxLogProviderType.Serilog;
protected override IMvxApplication CreateApp()
{
throw new NotImplementedException();
}
protected override IMvxLogProvider CreateLogProvider()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File($"{Directory.GetCurrentDirectory()}\\my_log.log")
.CreateLogger();
return base.CreateLogProvider();
}
}
App.xaml.cs 是标准的我在引入记录器时没有改变任何东西:
App.xaml.cs is standard I haven't change anything when introducing the logger:
public partial class App : MvxApplication
{
protected override void RegisterSetup()
{
this.RegisterSetupType<MvxWpfSetup<Core.App>>();
}
}
在 App.cs 的 Project.Core 中,我只注册应用程序启动:
In Project.Core in App.cs I register app start only:
public class App : MvxApplication
{
public override void Initialize()
{
RegisterAppStart<LoginViewModel>();
}
}
在 ViewModel 的 Project.Core 中,我使用构造函数注入来获取记录器:
In Project.Core in ViewModel I use constructor injection to obtain the logger:
public class LoginViewModel : MvxViewModel
{
#region Fields
private readonly IMvxLog _log;
#endregion
public LoginViewModel(IMvxNavigationService navigationService, IMvxLogProvider log)
{
_log = log.GetLogFor<LoginViewModel>();
}
}
当我使用 _log.Info("Log this information.");在调试模式下,我看到记录器是彩色控制台"而不是 Serilog - 从未执行过 Setup.cs 并且未创建记录器.
When I use _log.Info("Log this information."); in Debug mode I see that the logger is 'Coloured Console' instead of Serilog - Setup.cs was never executed and the logger wasn't created.
问题:我应该如何使用 Setup.cs?需要初始化吗?我在这里错过了什么?
Question: How should I use Setup.cs? Does it require initialisation? What am I missing here?
问候,卡罗尔
推荐答案
答案就是 App.xaml.cs 中的那一行:
The answer is that line in App.xaml.cs:
MvxWpfSetupSingleton.EnsureSingletonAvailable(Dispatcher, MainWindow);
它初始化"Setup.cs 和同样的记录器.
It 'initializes' the Setup.cs and the same the logger.
这篇关于MvvmCross 7 如何在 WPF (.net Core 3.1) 应用程序中设置 Serilog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!