class Program { static void Main(string[] args) { var services = new ServiceCollection(); ConfigureServices(services); using (ServiceProvider serviceProvider = services.BuildServiceProvider()) { MyApplication app = serviceProvider.GetService<MyApplication>(); app.Run(); } } private static void ConfigureServices(ServiceCollection services) { services.AddLogging(); services.AddTransient<MyApplication>(); } }
public class MyApplication { private readonly ILogger _logger; public MyApplication(ILogger<MyApplication> logger) { _logger = logger; } public void Run() { Console.WriteLine("hello world"); _logger.LogInformation("hello world , this is the log"); } }