-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
90 lines (81 loc) · 2.81 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
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using System.Windows.Forms;
namespace host
{
public class MainForm : Form
{
public MainForm(Action close)
{
this.FormClosed += (o, e) => close();
var button = new Button();
Controls.Add(button);
button.Text = "OK";
button.Click += (o, e) =>
{
Text = DateTime.Now.ToString("s");
new Form().Show();
};
}
}
public class App : IHostedService
{
private readonly ILogger<App> logger;
private readonly IHostApplicationLifetime appLifetime;
public App(ILogger<App> logger, IHostApplicationLifetime appLifetime)
{
this.logger = logger;
this.appLifetime = appLifetime;
}
public Task StartAsync(CancellationToken cancellationToken)
{
logger.LogCritical("StartAsync...");
Task.Run(async () =>
{
while (!cancellationToken.IsCancellationRequested)
{
logger.LogInformation("{date}", DateTime.Now);
await Task.Delay(TimeSpan.FromSeconds(1));
}
}, cancellationToken);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Task.Run(() => Application.Run(new MainForm(() => appLifetime.StopApplication())));
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
logger.LogError("StopAsync...");
return Task.CompletedTask;
}
}
class Program
{
[STAThread]
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs\\app.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
var host = CreateHostBuilder(args).Build();
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<App>();
}).ConfigureLogging(c =>
{
c.ClearProviders();
c.AddSerilog();
});//.UseConsoleLifetime();
}
}