-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
78 lines (70 loc) · 2.83 KB
/
Program.cs
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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NLog;
using NLog.Extensions.Logging;
using Swagger2Doc.Services;
using Swagger2Doc.Startup;
namespace Swagger2Doc
{
internal class Program
{
static async Task Main(string[] args)
{
#region NLog Init
string configuringFileName = "NLog.config";
// WebHost : ASPNETCORE_ENVIRONMENT
// Console : DOTNET_ENVIRONMENT
string? aspnetEnvironment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
string environmentSpecificLogFileName = $"NLog.{aspnetEnvironment}.config";
if (File.Exists(environmentSpecificLogFileName))
{
configuringFileName = environmentSpecificLogFileName;
}
Logger logger = NLog.LogManager.Setup().LoadConfigurationFromFile(configuringFileName).GetCurrentClassLogger();
#endregion
try
{
IHostBuilder host = Host.CreateDefaultBuilder(args);
// NLog Setup
host.ConfigureLogging((hostContext, logging) =>
{
// ensure just use NLog
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
logging.AddNLog(configuringFileName);
});
// Config Setup
host.ConfigureAppSetting();
// DI Cus Services
host.ConfigureServices((hostContext, services) =>
{
services.AddTransient<ConvertService>();
});
host.UseConsoleLifetime();
IHost app = host.Build();
logger.Info("Host Instance Builed:" + aspnetEnvironment);
// Run Cus Services
using (var serviceScope = app.Services.CreateScope())
{
IServiceProvider serviceProvider = serviceScope.ServiceProvider;
ConvertService service = serviceProvider.GetService<ConvertService>() ?? throw new ArgumentNullException(nameof(ConvertService));
service.Run();
}
}
catch (Exception ex)
{
// NLog: catch any exception and log it.
logger.Error(ex, $"Stopped program because of exception: {JsonConvert.SerializeObject(ex)}");
throw;
}
finally
{
logger.Info("program Shutdown");
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
LogManager.Shutdown();
}
}
}
}