Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any examples of using the new logging system in MvvmCross 6


Are there any examples of using the new logging system in MvvmCross 6


In MvvmCross 6, the logging system has been significantly improved and simplified. The old `MvxTrace` and related methods have been removed, and a new logging system based on `IMvxLog` and `IMvxLogProvider` was introduced. However, for MvvmCross 8 and later, the framework uses `Microsoft.Extensions.Logging`, which provides a more standardized and flexible logging API. Here's how you can use the new logging system in MvvmCross 6 and later versions:

MvvmCross 6 and 7

In these versions, you would typically use `IMvxLog` and `IMvxLogProvider`. Here’s how you can implement logging in a ViewModel:

csharp
public class MyViewModel : MvxViewModel
{
    private readonly IMvxLog _log;

    public MyViewModel(IMvxLogProvider logProvider)
    {
        _log = logProvider.GetLogFor();
    }

    private void SomeMethod()
    {
        _log.ErrorException("Some message", new Exception());
    }
}

To use logging outside of a ViewModel, such as in a View or Model, you can resolve the `IMvxLogProvider` using the IoC container:

csharp
var logProvider = Mvx.IoCProvider.Resolve();
var log = logProvider.GetLogFor();
log.Trace("Some message");

MvvmCross 8 and Later

In MvvmCross 8 and later, the logging system is based on `Microsoft.Extensions.Logging`. You can inject either `ILogger` or `ILoggerFactory` into your classes. Here’s how to use it in a ViewModel:

csharp
public class MyViewModel : MvxViewModel
{
    private readonly ILogger _logger;

    public MyViewModel(ILogger logger)
    {
        _logger = logger;
    }

    private void SomeMethod()
    {
        _logger.Log(LogLevel.Trace, "Hello, World");
    }
}

To customize logging, you can use third-party providers like Serilog. You need to override `CreateLogProvider` and `CreateLogFactory` in your `Setup.cs`:

csharp
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 for structured logging and supports various sinks for logging to different outputs like files, console, or even specific platforms like Android or iOS.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/logging-new
[2] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[3] https://stackoverflow.com/questions/53341932/mvvmcross-use-mvxlog-outside-viewmodel
[4] https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-60
[5] https://github.com/MvvmCross/MvvmCross-Samples/blob/master/TipCalc/TipCalc.WPF/Setup.cs
[6] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[7] https://www.mvvmcross.com/logging
[8] https://www.mvvmcross.com/documentation/getting-started/getting-started
[9] https://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_6003.htm
[10] https://stackoverflow.com/questions/68075159/mvvm-cross-logging-setup-class
[11] https://www.mvvmcross.com/mvvmcross-6.0.0-release/