The switch to Microsoft.Extensions.Logging in MvvmCross, starting from version 8, has significantly impacted how logging is handled within the framework. Here's a detailed overview of these changes:
Overview of Microsoft.Extensions.Logging
**Microsoft.Extensions.Logging provides a standardized logging interface that allows developers to write code with a common logging API while using various logging frameworks behind the scenes. This includes built-in providers like console, debug, event log, and Azure App Services logging, as well as support for third-party providers such as NLog, log4net, and Serilog[4].
Impact on MvvmCross
1. Replacement of Custom Logging Interface: Prior to MvvmCross 8, the framework used its own custom logging interface (`IMvxLog` and `IMvxLogProvider`). With the switch to Microsoft.Extensions.Logging, these custom interfaces are no longer used. Instead, MvvmCross now relies on `ILogger` and `ILoggerFactory` for logging purposes[1].
2. Dependency Injection: The new logging system integrates well with MvvmCross's Inversion of Control (IoC) container. Developers can inject `ILogger` or `ILoggerFactory` into their classes, such as ViewModels, Services, or Repositories, using the MvvmCross IoC provider. This allows for scoped logging, which is beneficial for filtering logs by specific components like ViewModels[1].
3. Custom Setup Class Requirement: In MvvmCross 8, developers must create their own `MvxSetup` derivative class. This class requires implementing methods to create an `ILoggerProvider` and an `ILoggerFactory`, which are essential for setting up the logging infrastructure[10]. This change ensures that logging is explicitly configured for each application.
4. Flexibility and Extensibility: The use of Microsoft.Extensions.Logging provides flexibility in choosing logging providers. Developers can easily add third-party logging frameworks by implementing the necessary providers and factories. For example, using Serilog as a logging provider involves creating a `SerilogLoggerProvider` and configuring Serilog in the `CreateLogFactory` method[5].
5. Breaking Changes: The transition to Microsoft.Extensions.Logging introduced breaking changes, requiring developers to update their logging setup when upgrading from MvvmCross 7 to version 8 or later. This includes removing deprecated logging classes like `MvxTrace` and adapting to the new logging API[2][10].
Example Implementation
To implement logging in MvvmCross 8, you would typically create a custom `Setup` class that inherits from a platform-specific setup class (e.g., `MvxWpfSetup` for WPF). In this class, you override methods to create an `ILoggerProvider` and an `ILoggerFactory`. Here's a simplified example using Serilog:
csharp
using Microsoft.Extensions.Logging;
using Serilog;
public class Setup : MvxWpfSetup
{
protected override ILoggerProvider CreateLogProvider()
{
return new SerilogLoggerProvider();
}
protected override ILoggerFactory CreateLogFactory()
{
Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().CreateLogger();
return new SerilogLoggerFactory();
}
}
This setup allows you to use Serilog as your logging provider while still benefiting from the standardized logging interface provided by Microsoft.Extensions.Logging.
Citations:[1] https://www.mvvmcross.com/documentation/fundamentals/logging-new
[2] https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-60
[3] https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Core/MvxSetup.cs
[4] https://putridparrot.com/blog/using-microsoft-extensions-logging/
[5] https://stackoverflow.com/questions/68075159/mvvm-cross-logging-setup-class
[6] https://stackoverflow.com/questions/39587974/changes-in-mvvmcross-bindings
[7] https://stackoverflow.com/questions/64831246/using-multiple-microsoft-extension-logging-iloggers-in-prism-xamarin-forms
[8] https://www.mvvmcross.com/mvvmcross-8.0.0-release/
[9] https://www.mvvmcross.com/documentation/tutorials/tipcalc/a-wpf-ui-project
[10] https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-80
[11] https://www.nuget.org/packages/MvvmCross/9.3.0-alpha.104