Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does the new logging system in MvvmCross handle logging scopes


How does the new logging system in MvvmCross handle logging scopes


The new logging system in MvvmCross, introduced in version 8, utilizes the Microsoft.Extensions.Logging framework. This framework supports logging scopes, which are useful for grouping related log messages together. Here's how logging scopes are handled:

Overview of Logging Scopes

Logging scopes allow you to attach additional context to log messages. This context can include information like user IDs, transaction IDs, or any other relevant data that helps in identifying the source or purpose of the log messages. Scopes are particularly useful when you need to track a series of operations that are logically related, such as processing a transaction.

Implementing Logging Scopes

To use logging scopes in MvvmCross with the Microsoft.Extensions.Logging framework, you typically create a scope using the `BeginScope` method of an `ILogger` instance. This method returns an `IDisposable` object that represents the scope. The scope remains active until it is disposed of, usually at the end of a `using` block.

Here is an example of how you might use a logging scope in a ViewModel:

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

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

    public void SomeMethod()
    {
        var userId = "12345";
        using (_logger.BeginScope(new List>
        {
            new KeyValuePair("UserId", userId)
        }))
        {
            _logger.LogInformation("Starting some operation");
            // Perform some operation
            _logger.LogInformation("Finished some operation");
        }
    }
}

In this example, all log messages within the `using` block will include the `UserId` context, which can be very useful for filtering or analyzing logs later.

Benefits of Logging Scopes

1. Contextual Logging: Scopes allow you to add contextual information to your logs, making it easier to understand the flow of your application and diagnose issues.
2. Filtering and Analysis: With additional context, you can more easily filter logs to focus on specific parts of your application or specific user interactions.
3. Structured Logging: When combined with structured logging providers like Serilog, scopes can enhance the structured logging capabilities by including additional fields in the log output.

Integration with Third-Party Providers

MvvmCross's logging system is designed to work seamlessly with third-party logging providers such as Serilog. These providers often support structured logging and can output logs to various destinations like files, databases, or cloud services. By using these providers, you can further customize how your logs are handled and where they are stored.

To integrate a provider like Serilog, you would typically override the `CreateLogProvider` and `CreateLogFactory` methods in your `MvxSetup` class to configure Serilog as the logging provider. This allows you to leverage Serilog's features, including its support for logging scopes and structured logging.

csharp
protected override ILoggerProvider CreateLogProvider()
{
    return new SerilogLoggerProvider();
}

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

This setup ensures that your application's logs are handled by Serilog, which supports logging scopes and can output logs in a structured format to various destinations.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/logging-new
[2] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[3] https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Core/MvxSetup.cs
[4] https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-9.0
[5] https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-60
[6] https://github.com/MvvmCross/MvvmCross/issues/1649
[7] https://www.mvvmcross.com/documentation/fundamentals/inversion-of-control-ioc
[8] https://www.mezmo.com/learn-log-management/system-logging-best-practices
[9] https://www.mvvmcross.com/logging
[10] https://stackoverflow.com/questions/68075159/mvvm-cross-logging-setup-class
[11] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview