using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Autofac.Extensions.DependencyInjection;
using NLog.Web;
using ASHermed.SinoInterface.Tools;
using Microsoft.AspNetCore;
using Steeltoe.Common.Hosting;

namespace ASHermed.SinoInterface.WebApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
            //CreateHostBuilder(args).Build().Run();
            BuildWebHost(args).Run();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseCloudHosting(APIConfig.GetInstance().Port)//部署在linux上才生效，接口的端口配置
                .UseStartup<Startup>()
                .UseUrls($"http://0.0.0.0:{APIConfig.GetInstance().Port}")//部署在linux上才生效，接口的端口配置
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Information);
                })
                .UseNLog()//为依赖注入设置NLog
                .Build();

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>()
                        .ConfigureLogging(logging =>
                              {
                                  logging.ClearProviders()
                                         .SetMinimumLevel(LogLevel.Information)
                                         .AddConsole(x=>x.TimestampFormat= "[yyyy-MM-dd HH:mm:ss]");
                              })
                              .UseNLog();
                })
                .UseServiceProviderFactory(new AutofacServiceProviderFactory());
    }
}
