Logging, Configuration
This commit is contained in:
@@ -1,26 +1,32 @@
|
|||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls.ApplicationLifetimes;
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
using FileTime.App.Core;
|
using FileTime.App.Core;
|
||||||
using FileTime.Avalonia.ViewModels;
|
using FileTime.Avalonia.ViewModels;
|
||||||
using FileTime.Avalonia.Views;
|
using FileTime.Avalonia.Views;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace FileTime.Avalonia
|
namespace FileTime.Avalonia
|
||||||
{
|
{
|
||||||
public class App : global::Avalonia.Application
|
public class App : global::Avalonia.Application
|
||||||
{
|
{
|
||||||
public static IServiceProvider ServiceProvider { get; private set; }
|
public static IServiceProvider ServiceProvider { get; }
|
||||||
|
|
||||||
static App()
|
static App()
|
||||||
{
|
{
|
||||||
ServiceProvider ??= DependencyInjection
|
ServiceProvider ??= DependencyInjection
|
||||||
.RegisterDefaultServices()
|
.RegisterDefaultServices()
|
||||||
|
.AddConfiguration()
|
||||||
|
.InitSerilog()
|
||||||
|
.RegisterLogging()
|
||||||
.AddViewModels()
|
.AddViewModels()
|
||||||
.AddServices()
|
.AddServices()
|
||||||
.RegisterCommandHandlers()
|
.RegisterCommandHandlers()
|
||||||
.BuildServiceProvider();
|
.BuildServiceProvider();
|
||||||
|
|
||||||
|
var _logger = ServiceProvider.GetService<ILogger<App>>();
|
||||||
|
_logger?.LogInformation("App initialization completed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace FileTime.Avalonia.Configuration
|
||||||
|
{
|
||||||
|
public static class MainConfiguration
|
||||||
|
{
|
||||||
|
public static readonly Dictionary<string, string> Configuration = new();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Models\" />
|
<Folder Include="Models\" />
|
||||||
<AvaloniaResource Include="Assets\**" />
|
<AvaloniaResource Include="Assets\**" />
|
||||||
|
<Folder Include="Configuration\" />
|
||||||
<None Remove=".gitignore" />
|
<None Remove=".gitignore" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -21,9 +22,14 @@
|
|||||||
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.12" />
|
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.12" />
|
||||||
<PackageReference Include="Avalonia.Svg.Skia" Version="0.10.12" />
|
<PackageReference Include="Avalonia.Svg.Skia" Version="0.10.12" />
|
||||||
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.12" />
|
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.12" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
|
||||||
<PackageReference Include="MvvmGen" Version="1.1.2" />
|
<PackageReference Include="MvvmGen" Version="1.1.2" />
|
||||||
|
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||||
<PackageReference Include="Syroot.Windows.IO.KnownFolders" Version="1.2.3" />
|
<PackageReference Include="Syroot.Windows.IO.KnownFolders" Version="1.2.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,18 +1,85 @@
|
|||||||
using System;
|
using System.Reflection;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Controls.ApplicationLifetimes;
|
|
||||||
using Avalonia.ReactiveUI;
|
using Avalonia.ReactiveUI;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace FileTime.Avalonia
|
namespace FileTime.Avalonia
|
||||||
{
|
{
|
||||||
class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
public static string AppDataRoot { get; }
|
||||||
|
public static string EnvironmentName { get; }
|
||||||
|
|
||||||
|
static Program()
|
||||||
|
{
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
EnvironmentName = "Development";
|
||||||
|
|
||||||
|
AppDataRoot = Path.Combine(Environment.CurrentDirectory, "appdata");
|
||||||
|
#else
|
||||||
|
EnvironmentName = "Release";
|
||||||
|
|
||||||
|
var possibleDataRootsPaths = new List<string>()
|
||||||
|
{
|
||||||
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FileTime"),
|
||||||
|
Path.Combine(Assembly.GetEntryAssembly()?.Location ?? ".", "fallbackDataRoot")
|
||||||
|
};
|
||||||
|
|
||||||
|
string? appDataRoot = null;
|
||||||
|
foreach (var possibleAppDataRoot in possibleDataRootsPaths)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var appDataRootDirectory = new DirectoryInfo(possibleAppDataRoot);
|
||||||
|
if (!appDataRootDirectory.Exists) appDataRootDirectory.Create();
|
||||||
|
|
||||||
|
//TODO write test
|
||||||
|
appDataRoot = possibleAppDataRoot;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (appDataRoot == null) throw new UnauthorizedAccessException();
|
||||||
|
AppDataRoot = appDataRoot;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
||||||
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
||||||
// yet and stuff might break.
|
// yet and stuff might break.
|
||||||
[STAThread]
|
[STAThread]
|
||||||
public static void Main(string[] args) => BuildAvaloniaApp()
|
public static void Main(string[] args)
|
||||||
.StartWithClassicDesktopLifetime(args);
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
var message = $"Ciritcal error cought in {nameof(Program)}";
|
||||||
|
if (App.ServiceProvider?.GetService<ILogger<Program>>() is var logger && logger != null)
|
||||||
|
{
|
||||||
|
logger.LogCritical(0, e, message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var logsPath = Path.Combine(AppDataRoot, "logs");
|
||||||
|
var logsDirectory = new DirectoryInfo(logsPath);
|
||||||
|
if (!logsDirectory.Exists) logsDirectory.Create();
|
||||||
|
|
||||||
|
var logPath = Path.Combine(logsPath, "_criticalError.log");
|
||||||
|
|
||||||
|
using var fileWriter = File.Open(logPath, FileMode.OpenOrCreate, FileAccess.Write);
|
||||||
|
using var streamWriter = new StreamWriter(fileWriter);
|
||||||
|
streamWriter.WriteLine(DateTime.Now.ToString() + ": " + message + "\n" + e.ToString() + "\n\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Avalonia configuration, don't remove; also used by visual designer.
|
// Avalonia configuration, don't remove; also used by visual designer.
|
||||||
public static AppBuilder BuildAvaloniaApp()
|
public static AppBuilder BuildAvaloniaApp()
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace FileTime.Avalonia.Services
|
|||||||
_itemNameConverterService = itemNameConverterService;
|
_itemNameConverterService = itemNameConverterService;
|
||||||
_contentProviders = contentProviders;
|
_contentProviders = contentProviders;
|
||||||
_localContentProvider = localContentProvider;
|
_localContentProvider = localContentProvider;
|
||||||
_settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FileTime", "savedState.json");
|
_settingsPath = Path.Combine(Program.AppDataRoot, "savedState.json");
|
||||||
|
|
||||||
_jsonOptions = new JsonSerializerOptions()
|
_jsonOptions = new JsonSerializerOptions()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
using System.Runtime.InteropServices;
|
using System.IO;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using FileTime.Avalonia.Application;
|
using FileTime.Avalonia.Application;
|
||||||
|
using FileTime.Avalonia.Configuration;
|
||||||
using FileTime.Avalonia.IconProviders;
|
using FileTime.Avalonia.IconProviders;
|
||||||
using FileTime.Avalonia.Services;
|
using FileTime.Avalonia.Services;
|
||||||
using FileTime.Avalonia.ViewModels;
|
using FileTime.Avalonia.ViewModels;
|
||||||
using FileTime.Core.Command;
|
using FileTime.Core.Command;
|
||||||
using FileTime.Core.Interactions;
|
using FileTime.Core.Interactions;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace FileTime.Avalonia
|
namespace FileTime.Avalonia
|
||||||
{
|
{
|
||||||
@@ -21,7 +25,6 @@ namespace FileTime.Avalonia
|
|||||||
internal static IServiceCollection AddServices(this IServiceCollection serviceCollection)
|
internal static IServiceCollection AddServices(this IServiceCollection serviceCollection)
|
||||||
{
|
{
|
||||||
serviceCollection = serviceCollection
|
serviceCollection = serviceCollection
|
||||||
.AddLogging()
|
|
||||||
.AddSingleton<ItemNameConverterService>()
|
.AddSingleton<ItemNameConverterService>()
|
||||||
.AddSingleton<StatePersistenceService>()
|
.AddSingleton<StatePersistenceService>()
|
||||||
.AddSingleton<IIconProvider, MaterialIconProvider>();
|
.AddSingleton<IIconProvider, MaterialIconProvider>();
|
||||||
@@ -39,12 +42,47 @@ namespace FileTime.Avalonia
|
|||||||
}
|
}
|
||||||
internal static IServiceCollection RegisterCommandHandlers(this IServiceCollection serviceCollection)
|
internal static IServiceCollection RegisterCommandHandlers(this IServiceCollection serviceCollection)
|
||||||
{
|
{
|
||||||
foreach (var commandHandler in FileTime.Providers.Local.Startup.GetCommandHandlers())
|
foreach (var commandHandler in Providers.Local.Startup.GetCommandHandlers())
|
||||||
{
|
{
|
||||||
serviceCollection.AddTransient(typeof(ICommandHandler), commandHandler);
|
serviceCollection.AddTransient(typeof(ICommandHandler), commandHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
return serviceCollection;
|
return serviceCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static IServiceCollection RegisterLogging(this IServiceCollection serviceCollection)
|
||||||
|
{
|
||||||
|
return serviceCollection.AddLogging(loggingBuilder =>
|
||||||
|
loggingBuilder.AddSerilog(dispose: true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static IServiceCollection AddConfiguration(this IServiceCollection serviceCollection)
|
||||||
|
{
|
||||||
|
var configuration = new ConfigurationBuilder()
|
||||||
|
.AddInMemoryCollection(MainConfiguration.Configuration)
|
||||||
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||||
|
.AddJsonFile($"appsettings.{Program.EnvironmentName}.json", true, true)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
return serviceCollection
|
||||||
|
//.Configure<SzopiAPIConfig>(configuration.GetSection("server"))
|
||||||
|
.AddSingleton<IConfiguration>(configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static IServiceCollection InitSerilog(this IServiceCollection serviceCollection)
|
||||||
|
{
|
||||||
|
Log.Logger = new LoggerConfiguration()
|
||||||
|
.ReadFrom.Configuration(serviceCollection.BuildServiceProvider().GetService<IConfiguration>())
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.WriteTo.File(
|
||||||
|
Path.Combine(Program.AppDataRoot, "logs", "appLog.log"),
|
||||||
|
fileSizeLimitBytes: 10*1024*1024,
|
||||||
|
rollOnFileSizeLimit: true,
|
||||||
|
rollingInterval: RollingInterval.Day)
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
|
return serviceCollection;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ using FileTime.Core.Providers;
|
|||||||
using Syroot.Windows.IO;
|
using Syroot.Windows.IO;
|
||||||
using FileTime.Avalonia.IconProviders;
|
using FileTime.Avalonia.IconProviders;
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace FileTime.Avalonia.ViewModels
|
namespace FileTime.Avalonia.ViewModels
|
||||||
{
|
{
|
||||||
@@ -34,6 +35,7 @@ namespace FileTime.Avalonia.ViewModels
|
|||||||
[Inject(typeof(AppState), PropertyAccessModifier = AccessModifier.Public)]
|
[Inject(typeof(AppState), PropertyAccessModifier = AccessModifier.Public)]
|
||||||
[Inject(typeof(StatePersistenceService), PropertyName = "StatePersistence", PropertyAccessModifier = AccessModifier.Public)]
|
[Inject(typeof(StatePersistenceService), PropertyName = "StatePersistence", PropertyAccessModifier = AccessModifier.Public)]
|
||||||
[Inject(typeof(ItemNameConverterService))]
|
[Inject(typeof(ItemNameConverterService))]
|
||||||
|
[Inject(typeof(ILogger<MainPageViewModel>), PropertyName = "_logger")]
|
||||||
public partial class MainPageViewModel : IMainPageViewModelBase
|
public partial class MainPageViewModel : IMainPageViewModelBase
|
||||||
{
|
{
|
||||||
const string RAPIDTRAVEL = "rapidTravel";
|
const string RAPIDTRAVEL = "rapidTravel";
|
||||||
@@ -87,6 +89,7 @@ namespace FileTime.Avalonia.ViewModels
|
|||||||
|
|
||||||
async partial void OnInitialize()
|
async partial void OnInitialize()
|
||||||
{
|
{
|
||||||
|
_logger?.LogInformation($"Starting {nameof(MainPageViewModel)} initialization...");
|
||||||
_clipboard = App.ServiceProvider.GetService<IClipboard>()!;
|
_clipboard = App.ServiceProvider.GetService<IClipboard>()!;
|
||||||
_timeRunner = App.ServiceProvider.GetService<TimeRunner>()!;
|
_timeRunner = App.ServiceProvider.GetService<TimeRunner>()!;
|
||||||
_contentProviders = App.ServiceProvider.GetService<IEnumerable<IContentProvider>>()!;
|
_contentProviders = App.ServiceProvider.GetService<IEnumerable<IContentProvider>>()!;
|
||||||
@@ -205,6 +208,7 @@ namespace FileTime.Avalonia.ViewModels
|
|||||||
Places = places;
|
Places = places;
|
||||||
await Task.Delay(100);
|
await Task.Delay(100);
|
||||||
Loading = false;
|
Loading = false;
|
||||||
|
_logger?.LogInformation($"{nameof(MainPageViewModel)} initialized.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateParalellCommands(object? sender, EventArgs e)
|
private void UpdateParalellCommands(object? sender, EventArgs e)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ using FileTime.Avalonia.Misc;
|
|||||||
using FileTime.Avalonia.Models;
|
using FileTime.Avalonia.Models;
|
||||||
using FileTime.Avalonia.ViewModels;
|
using FileTime.Avalonia.ViewModels;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
@@ -14,6 +15,7 @@ namespace FileTime.Avalonia.Views
|
|||||||
{
|
{
|
||||||
public partial class MainWindow : Window
|
public partial class MainWindow : Window
|
||||||
{
|
{
|
||||||
|
private readonly ILogger<MainWindow>? _logger;
|
||||||
public MainPageViewModel? ViewModel
|
public MainPageViewModel? ViewModel
|
||||||
{
|
{
|
||||||
get => DataContext as MainPageViewModel;
|
get => DataContext as MainPageViewModel;
|
||||||
@@ -30,6 +32,8 @@ namespace FileTime.Avalonia.Views
|
|||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
|
_logger = App.ServiceProvider.GetService<ILogger<MainWindow>>();
|
||||||
|
_logger?.LogInformation($"Starting {nameof(MainWindow)} initialization...");
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
this.AttachDevTools();
|
this.AttachDevTools();
|
||||||
@@ -123,6 +127,7 @@ namespace FileTime.Avalonia.Views
|
|||||||
{
|
{
|
||||||
if (ViewModel is not MainPageViewModel)
|
if (ViewModel is not MainPageViewModel)
|
||||||
{
|
{
|
||||||
|
_logger?.LogInformation($"{nameof(MainWindow)} opened, starting {nameof(MainPageViewModel)} initialization...");
|
||||||
ViewModel = App.ServiceProvider.GetService<MainPageViewModel>();
|
ViewModel = App.ServiceProvider.GetService<MainPageViewModel>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/GuiApp/FileTime.Avalonia/appsettings.Development.json
Normal file
11
src/GuiApp/FileTime.Avalonia/appsettings.Development.json
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"MinimalLevel": {
|
||||||
|
"Default": "Debug",
|
||||||
|
"Override": {
|
||||||
|
"Microsoft": "Information",
|
||||||
|
"System": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user