forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (68 loc) · 2.87 KB
/
Program.cs
File metadata and controls
74 lines (68 loc) · 2.87 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
using Autofac.Extensions.DependencyInjection;
using Blog.Core.Extensions.Apollo;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
namespace Blog.Core
{
public class Program
{
public static void Main(string[] args)
{
//初始化默认主机Builder
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseStartup<Startup>()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.Sources.Clear();
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
//.AddJsonFile($"appsettings{ GetAppSettingsConfigName() }json", optional: true, reloadOnChange: false)
;
//接入Apollo配置中心
config.AddConfigurationApollo("appsettings.apollo.json");
})
.UseUrls("http://*:9291")
.ConfigureLogging((hostingContext, builder) =>
{
// 1.过滤掉系统默认的一些日志
builder.AddFilter("System", LogLevel.Error);
builder.AddFilter("Microsoft", LogLevel.Error);
// 2.也可以在appsettings.json中配置,LogLevel节点
// 3.统一设置
builder.SetMinimumLevel(LogLevel.Error);
// 默认log4net.confg
builder.AddLog4Net(Path.Combine(Directory.GetCurrentDirectory(), "Log4net.config"));
})
;
})
// 生成承载 web 应用程序的 Microsoft.AspNetCore.Hosting.IWebHost。Build是WebHostBuilder最终的目的,将返回一个构造的WebHost,最终生成宿主。
.Build()
// 运行 web 应用程序并阻止调用线程, 直到主机关闭。
// ※※※※ 有异常,查看 Log 文件夹下的异常日志 ※※※※
.Run();
}
/// <summary>
/// 根据环境变量定向配置文件名称
/// </summary>
/// <returns></returns>
private static string GetAppSettingsConfigName()
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != null
&& Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != "")
{
return $".{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.";
}
else
{
return ".";
}
}
}
}