-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
73 lines (63 loc) · 2.37 KB
/
App.xaml.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
using Microsoft.Extensions.Hosting;
using System.Windows;
using AsaModCleaner.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace AsaModCleaner
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private readonly IHost _host;
public App()
{
// Determine the environment
var environment = IsDebug() ? "Development" : "Production";
// Set up the logger first, based on the environment
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Error()
.WriteTo.File($"logs\\{environment}_application_log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
_host = Host.CreateDefaultBuilder()
.UseSerilog() // Integrate Serilog into the Host for logging
.ConfigureAppConfiguration((context, config) =>
{
config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true);
})
.ConfigureServices((context, services) =>
{
services.AddSingleton<GameService>(); // Register GameService
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<MainWindow>(); // Register your main window with DI
})
.Build();
}
protected override async void OnStartup(StartupEventArgs e)
{
await _host.StartAsync();
// Show the main window using DI
var mainWindow = _host.Services.GetRequiredService<MainWindow>();
mainWindow.Show();
base.OnStartup(e);
}
protected override async void OnExit(ExitEventArgs e)
{
await _host.StopAsync();
await Log.CloseAndFlushAsync(); // Make sure logs are flushed before exit
base.OnExit(e);
}
private static bool IsDebug()
{
#if DEBUG
return true;
#else
return false;
#endif
}
}
}