forked from KawhiWei/Sukt.Admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (83 loc) · 4.61 KB
/
Program.cs
File metadata and controls
96 lines (83 loc) · 4.61 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 AspectCore.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using System;
using System.IO;
namespace Sukt.Core.API
{
public class Program
{
public static void Main(string[] args)
{
//Log.Logger = new LoggerConfiguration()
// .MinimumLevel.Information()
// .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
// .Enrich.FromLogContext()
// .WriteTo.Console()
// .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day)
// .CreateLogger();
//SeriLogLogger.SetSeriLoggerToFile("logs");
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
//.UseServiceContext()
.ConfigureWebHostDefaults(webBuilder =>
{
//如果API项目需要接入GRPC服务需要配置两个Kestrel主机,分别指定两个不通端口,因为GRPC默认是使用https
//webBuilder.ConfigureKestrel(opt =>
//{
// opt.ListenLocalhost(8852, o => o.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1);
// opt.ListenLocalhost(9852, o => o.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2);
//});
webBuilder.UseStartup<Startup>()
// .ConfigureKestrel(options =>
// {
//#if DEBUG
// options.ListenLocalhost(8361, o => o.Protocols =
// HttpProtocols.Http2);
// // ADDED THIS LINE to fix the problem
// options.ListenLocalhost(8001, o => o.Protocols =
// HttpProtocols.Http1);
//#else
// // ADDED THIS LINE to fix the problem
// options.ListenAnyIP(80, o => o.Protocols =
// HttpProtocols.Http1);
// options.ListenAnyIP(8331, o => o.Protocols =
// HttpProtocols.Http2);
//#endif
// })
.UseSerilog((webHost, configuration) =>
{
//得到配置文件
var serilog = webHost.Configuration.GetSection("Serilog");
//最小级别
var minimumLevel = serilog["MinimumLevel:Default"];
//日志事件级别
var logEventLevel = (LogEventLevel)Enum.Parse(typeof(LogEventLevel), minimumLevel);
configuration.ReadFrom.
Configuration(webHost.Configuration.GetSection("Serilog")).Enrich.FromLogContext().WriteTo.Console(logEventLevel);
configuration.WriteTo.Map(le => MapData(le),
(key, log) => log.Async(o => o.File(Path.Combine("Logs", @$"{key.time:yyyy-MM-dd}\{key.level.ToString().ToLower()}.txt"), logEventLevel)));
(DateTime time, LogEventLevel level) MapData(LogEvent logEvent)
{
return (new DateTime(logEvent.Timestamp.Year, logEvent.Timestamp.Month, logEvent.Timestamp.Day, logEvent.Timestamp.Hour, logEvent.Timestamp.Minute, logEvent.Timestamp.Second), logEvent.Level);
}
})//注入Serilog日志中间件//这里是配置log的
.ConfigureLogging((hostingContext, builder) =>
{
builder.ClearProviders();
builder.SetMinimumLevel(LogLevel.Information);
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
builder.AddConsole();
builder.AddDebug();
});
})
.UseDynamicProxy();//使用动态代理需要在Program引用此方法
}
}