androidxamarinmvvmcrossserilog

How to change Logger Configuration at Runtime using MvvmCross and Serilog on Android


I configured ILogger using Xamarin.Forms with MvvmCross like so:

public class Setup : MvxFormsAndroidSetup<App, UI.App>
{
    ...

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

    protected override ILoggerFactory CreateLogFactory()
    {
        Serilog.Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Is(_logEventLevel)
            .CreateLogger();

        return new SerilogLoggerFactory();
    }
    ...

Where _logEventLevel has a default value and everything logs as expected. At runtime I'm changing this value and want to register a new Logger. How do i register a new LoggerConfiguration with my updated _logEventLevel?

I tried calling MvxSetup.InitializeLoggingServices() but it still won't log the updated MinimumLevel.

Any idea?


Solution

  • So from the comments you say you want to change the configuration because you want to be able to change the log level during runtime. For that you don't need to instantiate a new configuration, you can use LogginLevelSwitch instead.

    So this would look something like:

    SeriLogLevelSwitch.MinimumLevel = LogEventLevel.Information;
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.ControlledBy(SeriLogLevelSwitch)
    

    Then later you can just change SeriLogLevelSwitch.MinimumLevel to something else:

    SeriLogLevelSwitch.MinimumLevel = LogEventLevel.Error;