Program.cs
3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Autofac.Extensions.DependencyInjection;
using HHECS.Dal;
using HHECS.WebCommon.Config;
using HHECS.WebCommon.SystemHelp.Log;
using HHECS.WebCommon.Port;
using log4net;
using log4net.Config;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.IO;
namespace HHECS.Web
{
public class Program
{
#region 全局变量
public static int httpPort { get; set; }
public static int httpsPort { get; set; }
#endregion
public static void Main(string[] args)
{
#region Nlog
InitLog4Net();
#endregion
InitSysValue();
#region 端口是否占用
var isHttpOk = ComputerHelp.PortInUse(httpPort);
if (isHttpOk)
{
Console.WriteLine("端口httpPort已被占用:" + httpPort);
return;
}
var isHttpsOk = ComputerHelp.PortInUse(httpsPort);
if (isHttpsOk)
{
Console.WriteLine("端口httpsPort已被占用:" + httpsPort);
return;
}
#endregion
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).UseServiceProviderFactory(new AutofacServiceProviderFactory());
}
private static void InitSysValue()
{
#region 获取配置信息
//添加 json 文件路径
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
//创建配置根对象
var configurationRoot = builder.Build();
//取 family 部分下的 mother 部分下的 name 部分
var appCustomSettings = configurationRoot.GetSection("AppCustomSettings");
DALHelper.Constr = configurationRoot.GetSection("ConnectionStrings").GetSection("BaseDBContext").Value;
httpPort = int.Parse(appCustomSettings.GetSection("WebPort").Value);
httpsPort = httpPort + 1000;
#endregion
string s = appCustomSettings.GetSection("LoginTitle").Value;
ConfigRead.GetInstance.GetAppsetConnection().LoginTitle = appCustomSettings.GetSection("LoginTitle").Value;
//内存缓存设置 路径和密码 定时器启动使用
ConfigRead.GetInstance.GetAppsetConnection().HttpWebPort = httpPort;
ConfigRead.GetInstance.GetAppsetConnection().HttpsWebPort = httpsPort;
}
private static void InitLog4Net()
{
//Log4NetHelper.Instance.Repository = LogManager.CreateRepository("NETCoreRepository");
var log4Config = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "Config", "log4net.config");
if (!File.Exists(log4Config))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("log4net.config文件路径不存在,请确认发布属性是【始终复制】:" + log4Config);
//Log4NetHelper.Instance.Info("Certificate path:" + log4Config);
Console.ReadLine();
}
//XmlConfigurator.Configure(Log4NetHelper.Instance.Repository, new FileInfo(log4Config));
Console.WriteLine("InitLog4Net Init Started successfully!");
}
}
}