在net core控制台程序中使用net core的日志组件Logging

    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");
        }
    }

 

posted @ 2020-07-02 15:28  向萧  阅读(626)  评论(0编辑  收藏  举报