Have Serilog get automatically the configuration
See original GitHub issueI just started using Serilog in a ASP.NET Core app, and I find it annoying that you cannot just use .UseSerilog()
but one has to explicitly pass in the configuration
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console())
Is there some technical matter that prevents the .UseSerilog
to just work? Why do I need to tell serilog to read the configuration? Cannot it be done automatically?
Also, I think that the “early initialization” example is going against the standard convention of ASP.NET Core.
The CreateDefaultBuilder
already creates the configuration object exactly as done in the sample. I don’t see why you would need to do it “manually”. I think as it is explained, it’s misleading.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Does Serilog captures log automatically when configured ...
The Log object is defined in Startup and "AddSerilog" is defined in "ConfigureServices" Method. But other than that no other log information is ......
Read more >A Serilog configuration provider that reads from Microsoft. ...
A Serilog settings provider that reads from Microsoft.Extensions.Configuration sources, including .NET Core's appsettings.json file. By default, configuration ...
Read more >Configure Serilog in ASP.NET Core - few practical tips
In one of the recent projects I worked we were using Serilog as the logging solution. The default configuration was not what worked...
Read more >Structured Logging Using Serilog In ASP.NET Core 7.0
The simplest way to configure Serilog in the application is by calling ReadFrom. Configuration(). We can also use the UseSerilogRequestLogging( ...
Read more >Serilog Best Practices - Ben Foster
We recommend using the configuration system since the logging configuration can be changed without releasing a new version of your application.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@simonech Howdy!
No technical matter; it could be done. As @sungam3r points out, the current layering would need to be reconsidered, but there’s no fundamental reason not to consider it.
Some complications will inevitably arise around how to customize configuration reading, (“how do I use a different section name?”, “how do I turn it off?”, perhaps) - and there’s the question of bloating the package dependency graph for those who don’t use
ReadFrom.Configuration()
, so it mostly needs thought and time/attention, but hasn’t made it to the top of the list thus far.Yes, this is deliberate - the default ASP.NET Core logging setup doesn’t collect exceptions from host setup, which I think is a mistake, although opinions vary and we try to support both approaches equally well. The post at https://nblumhardt.com/2019/10/serilog-in-aspnetcore-3/ discusses some of this. Let’s keep this particular ticket focused on your suggestion RE automatic reading of configuration so we don’t end up off down that rabbit hole 😃
I think we’re unlikely to tackle this in the immediate future, so closing, but will come back and review it if there’s a future opportunity to improve, here.