Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I customize the new logging system in MvvmCross 6


Can I customize the new logging system in MvvmCross 6


Customizing the new logging system in MvvmCross 6 involves using the Microsoft.Extensions.Logging framework, which was adopted starting from MvvmCross 8. However, since MvvmCross 6 does not use this framework directly, we'll focus on how logging was customized in MvvmCross 6 and then provide an overview of how it can be customized in later versions, including MvvmCross 8.

MvvmCross 6 Logging

In MvvmCross 6, the logging system was improved compared to earlier versions, but it still used the MvvmCross.Logging namespace. The `MvxTrace` and related classes were removed, and users were encouraged to use the new logging system available since MvvmCross 5.4.

To customize logging in MvvmCross 6, you would typically use the IMvxLog interface and extend it with custom providers like Serilog, NLog, etc., by overriding methods in the `Setup.cs` file of your platform-specific project.

Here's an example of how you might have customized logging in MvvmCross 6 using Serilog:

csharp
public class Setup : MvxAndroidSetup
{
    protected override IMvxLogProvider CreateLogProvider()
    {
        return new SerilogLogProvider();
    }
}

You would also need to configure Serilog in your application startup.

MvvmCross 8 and Later

Starting from MvvmCross 8, the logging system uses Microsoft.Extensions.Logging, which provides a more standardized and flexible way to handle logging.

Customizing Logging in MvvmCross 8

To customize logging in MvvmCross 8, you can use third-party providers like Serilog. Here’s how you can set it up:

1. Install NuGet Packages: You need to install the `Serilog` and `Serilog.Extensions.Logging` packages in your project.

2. Configure Logging in Setup.cs:

In your platform-specific `Setup.cs`, you need to override the `CreateLogProvider` and `CreateLogFactory` methods to use Serilog.

csharp
   public class Setup : MvxAndroidSetup
   {
       protected override ILoggerProvider CreateLogProvider()
       {
           return new SerilogLoggerProvider();
       }

       protected override ILoggerFactory CreateLogFactory()
       {
           // Configure Serilog
           Log.Logger = new LoggerConfiguration()
               .MinimumLevel.Debug()
               // Add sinks here, e.g., to log to files or console
               .WriteTo.Console()
               .CreateLogger();

           return new SerilogLoggerFactory();
       }
   }
   

3. Inject and Use Loggers:

You can inject an `ILogger` instance into your view models or services and use it for logging.

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

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

       public void SomeMethod()
       {
           _logger.LogInformation("Some log message");
       }
   }
   

Conclusion

Customizing the logging system in MvvmCross involves using the built-in interfaces and extending them with third-party providers. For MvvmCross 6, you would use the `IMvxLog` interface, while for MvvmCross 8 and later, you leverage the Microsoft.Extensions.Logging framework with providers like Serilog.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/logging-new
[2] https://www.mvvmcross.com/logging
[3] https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/CREATE-MATERIALIZED-VIEW-LOG.html
[4] https://stackoverflow.com/questions/68075159/mvvm-cross-logging-setup-class
[5] https://github.com/MvvmCross/MvvmCross-Samples/blob/master/StarWarsSample/StarWarsSample.Core/Logs.cs
[6] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[7] https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-60
[8] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[9] https://www.mvvmcross.com/documentation/advanced/customizing-appstart
[10] https://www.mvvmcross.com/mvvmcross-6.0.0-release/
[11] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup