Skip to content

Adds feature to completely reload serilog settings. Also contains switchable ILogger implementation.

License

Notifications You must be signed in to change notification settings

tagcode/serilog-settings-reloader

Repository files navigation

Serilog.Settings.Reloader

Serilog.Settings.Reloader provides completely reloadable settings for Serilog.

Links:

SwitchableLogger is assigned with new root ILogger when configuration is modified.

// Create switchable
SwitchableLogger switchableLogger = new SwitchableLogger();
// Assign SwitchableLogger to Serilog.Log.Logger
Serilog.Log.Logger = switchableLogger;

// Assign logger to switchableLogger
switchableLogger.Logger = new Serilog.LoggerConfiguration()
        .MinimumLevel.Verbose()
        .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {SourceContext}] {Message:lj}{NewLine}{Exception}")
        .CreateLogger();

// Create logger
ILogger logger = Serilog.Log.ForContext<Program>();

// Write
logger.Information("Hello World");

// Reconfigure 
ILogger newLogger = new Serilog.LoggerConfiguration()
        .MinimumLevel.Verbose()
        .WriteTo.Console(outputTemplate: "[{SourceContext}] {Message:lj}{NewLine}{Exception}")
        .CreateLogger();
// Assign new logger
switchableLogger.Set(newLogger, disposePrev: true);

// Write with the previous logger instance, but with different settings
logger.Information("Hello world again");



.AddSerilogConfigurationLoader() can be used with dependency injection's ILoggingBuilder.

// Read configuration
IConfigurationRoot configuration = new ConfigurationBuilder()
    Add(config)
    .Build();

// Service collection
IServiceCollection serviceCollection = new ServiceCollection()
    .AddLogging(loggingBuilder =>
        {
            SwitchableLogger switchableLogger = new SwitchableLogger();
            loggingBuilder
                .ClearProviders()
                .AddSerilog(switchableLogger, true)
                .AddSerilogConfigurationLoader(configuration, switchableLogger);
        });

// Services
using (var services = serviceCollection.BuildServiceProvider())
{
    // Create logger
    Microsoft.Extensions.Logging.ILogger logger = services.GetService<Microsoft.Extensions.Logging.ILogger<Program>>();

    // Write
    logger.LogInformation("Hello World");

    // Modify config
    config.Set("Serilog:WriteTo:0:Args:OutputTemplate", "[{SourceContext}] {Message:lj}{NewLine}{Exception}");
    configuration.Reload();

    // Write with the previous logger instance, but with different settings
    logger.LogInformation("Hello world again");
}



.AddSerilogConfigurationLoader(IConfiguration, SwitchableLogger, Func<IConfiguration, ILogger>) third argument specifies load function.

loggingBuilder
    .ClearProviders()
    .AddSerilog(switchableLogger, true)
    .AddSerilogConfigurationLoader(configuration, switchableLogger, 
        c => new Serilog.LoggerConfiguration().ReadFrom.Configuration(c).CreateLogger())
    );

About

Adds feature to completely reload serilog settings. Also contains switchable ILogger implementation.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages