C#C
C#3y ago
int

❔ How to log using hosts?

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

var builder = new HostApplicationBuilder();
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Services.AddLogging();

using var host = builder.Build();

var logger = host.Services.GetRequiredService<ILogger>();

logger.LogInformation("🙂");

await host.RunAsync();


the error is on when I try to get the logger service: Unhandled exception. System.InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILogger' has been registered.

i thought i had already registered the logging service: builder.Services.AddLogging();
Was this page helpful?