For the sample code at the start of the "Create logs in the Program class" section, CreateDefaultBuilder(args) should be Host.CreateDefaultBuilder(args) -- the Host portion is missing.
β Do not edit this section. It is required for docs.microsoft.com β GitHub issue linking.
Hello @MV10 ... Referring to the first example? It's ok ... it's calling CreateHostBuilder, which returns an IHostBuilder. It could add a little boilerplate, but that might be overkill.
... existing code for "public static void Main(string[] args)" ...
public static IHostBuilder CreateHostBuilder(string[] args)
{
...
}
It won't compile that way, Host. is missing:

It should be:
public static void Main(string[] args)
{
var host = Host.CreateHostBuilder(args).Build();
...etc.

Itβs not the complete example. The CreateHostBuilder method calls Host.CreateDefaultBuilder and returns an IHostBuilder. It can show the code I suggested. See the next example in this section. Iβm at the π gym πͺ on the treadmill ... hard to type a little more context for what Iβm proposing.
BACK! :smile: ... that treadmill nearly π me! ππππ
It's best if we show the recommended approach that calls CreateHostBuilder. So, here's a more fleshed-out example of what I propose for that example ...
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
var todoRepository = host.Services.GetRequiredService<ITodoRepository>();
todoRepository.Add(new Core.Model.TodoItem() { Name = "Feed the dog" });
todoRepository.Add(new Core.Model.TodoItem() { Name = "Walk the dog" });
var logger = host.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Seeded the database.");
IMyService myService = host.Services.GetRequiredService<IMyService>();
myService.WriteLog("Logged from MyService.");
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Doh! Can't believe I overlooked that. (I'm not a fan of the CreateHostBuilder approach since it doesn't work well for larger, real-world apps, so I'd forgotten MS does that everywhere.)
Most helpful comment
BACK! :smile: ... that treadmill nearly π me! ππππ
It's best if we show the recommended approach that calls
CreateHostBuilder. So, here's a more fleshed-out example of what I propose for that example ...