Here we use the most basic registration extension, AddHostBuilderLogger. The “Successful Run” SampleĪs the name suggests, this is the simplest example when everything goes right. Main demonstrating various scenarios, and the same five scenarios are also available in the ConsoleWithSerilog project. The ConsoleTest project in the repository has five variations on Program. You can read more about that at the end of the repository README. The project defines a couple of Generic Host background services to ensure the cache is flushed either at application startup or during a terminal failure event. It uses a thread-safe queue as the message cache, and adds some extension methods to make it easy to set up.īut the logger implementation isn’t the interesting part here, it’s what we do with the log messages that is important. Ironically, my solution is also basically a static logger, and since the general ILogger interface and extensions are so well-known, my solution replicates a subset of that functionality, supporting the basic Log () methods and related wrapper methods like LogInformation () and LogWarning (). ensuring logging works when logger configuration fails.ensuring logging works when application startup fails.writing cached messages to the host-provided logger.caching log messages prior to calling IHostBuilder.This means Serilog suffers from the same problems as the vanilla. Instead we use the optional IConfiguration-based approach so that logger behaviors can be changed without re-building the entire application. While I do use Serilog myself, I strongly dislike configuration in code, and in production apps we don’t use that approach. Quite frankly, this whole concern is a non-issue if you just use a static logger. Products like Serilog take the position that a static logger is better than a service-based logger. (This will be a separate package when I get around to setting up packaging.) The Basic Idea As I write this, it isn’t packaged on NuGet yet, but that should be coming soon.Īs a bonus, it optionally also supports Serilog, because Serilog is too awesome to ignore.
The code is in my repository, MV10/GenericHostBuilderLogger, and the README has everything you need to know to use it in your own application. This is a short article to introduce my solution to this problem. I decided to try doing something about it. On another project I’m doing a huge amount of work with hosted headless services, so I see this as a real problem. NET, this is bigger than just ASP.NET – the limitation applies to any Generic Host -based application. The discussion is pretty long (and isn’t entirely logging-focused), but suffice to say there isn’t a good solution. It’s not possible to log until after all of the services have been registered and the container has been built. …logging is built on dependency injection, and ConfigureLogging is just a wrapper for ConfigureServices. Using the generic host builder, how do you log?… A few weeks later, somebody noticed this means you can’t do logging during Program. The “Basic Exception With Config” Sampleīack in April of 2019, Microsoft announced a change to ASP.NET Core’s Startup class as a result of migrating the platform to the Generic Host model.The “Successful Run With Config” Sample.