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.

Have Serilog get automatically the configuration

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
nblumhardtcommented, Nov 22, 2019

@simonech Howdy!

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?

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.

Also, I think that the “early initialization” example is going against the standard convention of ASP.NET Core.

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 😃

0reactions
nblumhardtcommented, Jul 25, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

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