Skip to content

LoggingLevelSwitch Not Switching at the Run Time #452

Open
@balaji222528

Description

@balaji222528

Hi Team,
I have a requirement of switching the Minimum Log level at Run time .

consider in a MAUIProgram.Cs ham registering log like this

void RegisteRLog(IServiceCollection service collection, Configuration configuration)
{
LoggerConfiguration config = New LoggerConfiguration().ReadFrom.Configuration(configuration) //Read from App settings json

Log.Logger = config.MinimumLevel.ControlledBy(LoggingServiceInstance.LevelSwitch).CreateLogger();
LoggingService loggingservice = new();
serviceCollection.Addsingleton(logging service);
service collection.AddLogging(logging=>logging.AddSerilog(dispose:true))
}

public class LoggingService:ILoggingService
{
private static readonly Lazy instance = new(()=> new LoggignService());

public static LoggingService Instance => instance.Value;

public LoggingLevelSwitch LevelSwitch{get;}

public LoggingService()
{
LevelSwitch = new LoggingLevelSwitch();
}

public void SetMinimumLevel(LogEventLevel level)
{
LevelSwitch.MinimumLevel= level;
}
}

public class SomeViewModel
{
private ILoggingService _Loggingservice;
public SomeViewWmodel(ILoggingervice LoggingService)
{
_Loggingservice = LoggingService;
}

private void button click()
{
LogeventLevel level = LogEventLevel.Debug;
_loggingservice. SetMinimumLevel(level);
_log.LogDebug("test") // this line not printed.
}
}

Based on the above code iam not seeing the switch happening here at the run time and still the MinimumLevel in the. _log in the DI shows to the Information which is default. Not sure what is missing here. Any help Appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions