1
votes

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.

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.

Project structure is typical for MvvmCross: Project.Core .net Stardard 2.0; Project.Wpf .net Core 3.1

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 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>>();
    }
}

In Project.Core in App.cs I register app start only:

public class App : MvxApplication
{
    public override void Initialize()
    {
        RegisterAppStart<LoginViewModel>();
    }
}

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>();
    }
}

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.

Question: How should I use Setup.cs? Does it require initialisation? What am I missing here?

Regards, Karol

1

1 Answers

0
votes

The answer is that line in App.xaml.cs:

MvxWpfSetupSingleton.EnsureSingletonAvailable(Dispatcher, MainWindow);

It 'initializes' the Setup.cs and the same the logger.