wabbajack/Wabbajack.App.Blazor/App.xaml.cs

74 lines
2.3 KiB
C#
Raw Permalink Normal View History

2022-01-09 13:33:22 +00:00
using System;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
2022-01-21 13:54:22 +00:00
using NLog.Extensions.Logging;
using NLog.Targets;
2022-01-20 08:34:38 +00:00
using Wabbajack.App.Blazor.State;
using Wabbajack.App.Blazor.Utility;
2022-01-09 13:33:22 +00:00
using Wabbajack.Services.OSIntegrated;
2022-01-20 08:34:38 +00:00
namespace Wabbajack.App.Blazor;
public partial class App
2022-01-09 13:33:22 +00:00
{
2022-01-20 08:34:38 +00:00
private readonly IServiceProvider _serviceProvider;
public App()
2022-01-09 13:33:22 +00:00
{
2022-01-21 13:41:37 +00:00
_serviceProvider = Host.CreateDefaultBuilder(Array.Empty<string>())
2022-01-21 13:54:22 +00:00
.ConfigureLogging(SetupLogging)
2022-01-21 13:41:37 +00:00
.ConfigureServices(services => ConfigureServices(services))
.Build()
.Services;
2022-01-20 08:34:38 +00:00
}
2022-01-09 13:33:22 +00:00
2022-01-21 13:54:22 +00:00
private static void SetupLogging(ILoggingBuilder loggingBuilder)
{
var config = new NLog.Config.LoggingConfiguration();
var fileTarget = new FileTarget("file")
{
FileName = "log.log"
};
var consoleTarget = new ConsoleTarget("console");
var uiTarget = new MemoryTarget("ui");
2022-01-21 14:44:05 +00:00
var blackholeTarget = new NullTarget("blackhole");
2022-01-21 15:11:44 +00:00
if (!string.Equals("TRUE", Environment.GetEnvironmentVariable("DEBUG_BLAZOR", EnvironmentVariableTarget.Process), StringComparison.OrdinalIgnoreCase))
{
config.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Debug, blackholeTarget, "Microsoft.AspNetCore.Components.*", true);
}
2022-01-21 14:44:05 +00:00
2022-01-21 13:54:22 +00:00
config.AddRuleForAllLevels(fileTarget);
config.AddRuleForAllLevels(consoleTarget);
config.AddRuleForAllLevels(uiTarget);
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
loggingBuilder.AddNLog(config);
}
2022-01-20 08:34:38 +00:00
private static IServiceCollection ConfigureServices(IServiceCollection services)
{
services.AddOSIntegrated();
services.AddBlazorWebView();
services.AddTransient<MainWindow>();
services.AddSingleton<SystemParametersConstructor>();
2022-01-21 13:54:22 +00:00
services.AddSingleton<IStateContainer, StateContainer>();
2022-01-20 08:34:38 +00:00
return services;
}
2022-01-09 13:33:22 +00:00
2022-01-20 08:34:38 +00:00
private void OnStartup(object sender, StartupEventArgs e)
{
var mainWindow = _serviceProvider.GetRequiredService<MainWindow>();
2022-01-21 13:41:37 +00:00
mainWindow.Show();
2022-01-20 08:34:38 +00:00
}
private void OnExit(object sender, ExitEventArgs e)
{
Current.Shutdown();
2022-01-09 13:33:22 +00:00
}
2022-01-20 08:34:38 +00:00
}