question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Initialization loggerFactory limitations

See original GitHub issue

Is your feature request related to a problem? Please describe.

Currently some IHostBuilder extensions methods (e.g. AddKubernetesConfiguration(), AddConfigServer()) accepts an optional ILoggerFactory.

Because this code is evaluated before the DynamicSerilogProvider is created, the loggerFactory can only be configured using a static serilog configuration, i.e. its log level cannot be changed at runtime and cannot be configured from appsettings.

//create loggerFactory from a static configuration (not ideal)
ILoggerFactory initLogFactory = new LoggerFactory().AddSerilog(new LoggerConfiguration().MinimumLevel.Error().WriteTo.Console().CreateLogger());

return Host.CreateDefaultBuilder(args)
    .AddDynamicSerilog()
    .AddKubernetesConfiguration(null, initLogFactory) //initLogFactory is not configured with DynamicSerilog
    .AddConfigServer(initLogFactory) //initLogFactory is not configured with DynamicSerilog
    .AddDiscoveryClient()
    .AddAllActuators()
    .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());

Describe the solution you’d like

I would like to be able to pass loggerFactory in a lazy manner, so that I can build it when the DynamicSerilog is available. If that is not possible maybe those extension method may avoid to receive a loggerFactory as parameter, andthey could take it using DI instead (is it possible?).

Describe alternatives you’ve considered

Use a static Serilog LoggerConfiguration.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
hananielcommented, Jul 12, 2021

@DaviGia To clarify, the requirement has to do with the ILoggerFactory respecting the available configuration and not with the dynamically changing the log level via actuators. If you have a sample with the other config sources you mention that will be great.

1reaction
hananielcommented, Jul 9, 2021

@DaviGia I am wondering what the use case for this is. The logFactory is only used in the build phase. So your app is not ready and available for you to change configuration dynamically. Can you please clarify? Is it that it cannot be configured from app settings?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - What's the best way to initialise a logger in a class?
I've got two options to initialise a Logger within a class. The first way is providing an instance of the Class<?> : logger...
Read more >
SLF4J FAQ
More specifically, when initializing the LoggerFactory class will no longer search for the StaticLoggerBinder class on the class path.
Read more >
.NET Core Logging With LoggerFactory: Best Practices and ...
1. You could pass your existing LoggerFactory into every object/method you call (which is a terrible idea). 2. You could create your own...
Read more >
During testing, you must initialize the static LoggerFactory.
It strong limitations. "For the other presentation layers, you have to implement a startup logic anyway, where you then have to add the...
Read more >
Logging in .NET Core and ASP.NET Core
To control costs and not exceed data storage limits, log Trace , Debug , or Information level messages to a high-volume, low-cost data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found