.NET DependencyInjection and Serilog without using IHost (for console app)

Viewed 39

When you look at solutions for Dependency Injection and Serilog on .NET projects, most documentation and blogs refers to the IHost model. It does not matter much if for current .NET 6 or other versions of .NET Core. This is my favorite way for applications providing REST APIs since .NET Core 2.1.

I first defined a BuildLogger() method to configure Serilog with min log level and add all my enrichers/sinks I need.

So my Program.Main method used to look like:

Log.Logger = LoggerBuilder.BuildLogger();
var builder = Host.CreateDefaultBuilder(args).UseSerilog();  // serilog injected in Host.
builder.ConfigureServices(services => services.AddMyServices());
using var source = new CancellationTokenSource();
await builder.Build().RunAsync(source.Token).ConfigureAwait(false);

All fine.

Question is: how to do reuse DI and Serilog without using the IHost model from .NET 6? My today's user case if for a console app which is only running a few tests and then close, and still reusing some parts I wrote for my previous apps.

1 Answers

So instead of the above, I defined that

Log.Logger = LoggerBuilder.BuildLogger(); 
await using var serviceProvider = new ServiceCollection()
       .AddMyServices()
       .AddLogging(logBuilder => logBuilder.AddSerilog()) // attach serilog.
       .BuildServiceProvider()

RunTests(serviceProvider);   

This seems to work as expected.

The trick with AddLogging/AddSerilog() instead of UseSerilog() might be helpful for others!

Related