Skip to content

Instantly share code, notes, and snippets.

@jasterix
Last active December 5, 2021 17:28
Show Gist options
  • Save jasterix/abb824eaca7cfe7d7cce9b6c54bf82ef to your computer and use it in GitHub Desktop.
Save jasterix/abb824eaca7cfe7d7cce9b6c54bf82ef to your computer and use it in GitHub Desktop.
Create logs in Main -- The following code logs in Main by getting an ILogger instance from DI after building the host:
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
class Program
{
static Task Main(string[] args)
{
IHost host = Host.CreateDefaultBuilder(args).Build();
var logger = host.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Host created.");
return host.RunAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment