use serilog along with microsoft logger, rework config handling

This commit is contained in:
Oleksandr Bezdieniezhnykh
2024-07-20 18:06:58 +03:00
parent 83e3532de2
commit de60dd1989
4 changed files with 80 additions and 51 deletions
+32 -22
View File
@@ -8,6 +8,7 @@ using Azaion.Annotator.Extensions;
using LibVLCSharp.Shared;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using ILogger = Microsoft.Extensions.Logging.ILogger;
@@ -17,54 +18,63 @@ namespace Azaion.Annotator;
public partial class App : Application
{
private readonly ServiceProvider _serviceProvider;
private readonly IHost _host;
private readonly ILogger<App> _logger;
private readonly IMediator _mediator;
public App()
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Warning()
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File(
path: "Logs/log.txt",
rollingInterval: RollingInterval.Day)
.CreateLogger();
var services = new ServiceCollection();
services.AddSingleton<MainWindow>();
services.AddSingleton<HelpWindow>();
services.AddMediatR(c => c.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
services.AddSingleton<LibVLC>(_ => new LibVLC());
services.AddSingleton<FormState>();
services.AddSingleton<MediaPlayer>(sp =>
{
var libVLC = sp.GetRequiredService<LibVLC>();
return new MediaPlayer(libVLC);
});
services.AddSingleton<Config>(_ => Config.Read());
services.AddSingleton<PlayerControlHandler>();
_serviceProvider = services.BuildServiceProvider();
_mediator = _serviceProvider.GetService<IMediator>()!;
_host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddSingleton<MainWindow>();
services.AddSingleton<HelpWindow>();
services.AddMediatR(c => c.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
services.AddSingleton<LibVLC>(_ => new LibVLC());
services.AddSingleton<FormState>();
services.AddSingleton<MediaPlayer>(sp =>
{
var libVLC = sp.GetRequiredService<LibVLC>();
return new MediaPlayer(libVLC);
});
services.AddSingleton<IConfigRepository, FileConfigRepository>();
services.AddSingleton<Config>(sp => sp.GetRequiredService<IConfigRepository>().Get());
services.AddSingleton<PlayerControlHandler>();
})
.UseSerilog()
.Build();
_mediator = _host.Services.GetRequiredService<IMediator>();
_logger = _host.Services.GetRequiredService<ILogger<App>>();
DispatcherUnhandledException += OnDispatcherUnhandledException;
}
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Log.Logger.Error(e.Exception, e.Exception.Message);
_logger.LogError(e.Exception, e.Exception.Message);
e.Handled = true;
}
protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.KeyDownEvent, new RoutedEventHandler(GlobalClick));
_serviceProvider.GetRequiredService<MainWindow>().Show();
_host.Start();
_host.Services.GetRequiredService<MainWindow>().Show();
base.OnStartup(e);
}
private void GlobalClick(object sender, RoutedEventArgs e)
{
var args = (KeyEventArgs)e;
_mediator.Publish(new KeyEvent(sender, args));
}
}
+3
View File
@@ -14,11 +14,14 @@
<PackageReference Include="LibVLCSharp" Version="3.8.2" />
<PackageReference Include="LibVLCSharp.WPF" Version="3.8.2" />
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RangeTree" Version="3.0.1" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
+39 -25
View File
@@ -1,6 +1,7 @@
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Size = System.Windows.Size;
using Point = System.Windows.Point;
@@ -9,22 +10,10 @@ namespace Azaion.Annotator.DTO;
public class Config
{
private const string CONFIG_PATH = "config.json";
private const string DEFAULT_VIDEO_DIR = "video";
private const string DEFAULT_LABELS_DIR = "labels";
private const string DEFAULT_IMAGES_DIR = "images";
private const string DEFAULT_RESULTS_DIR = "results";
private static readonly Size DefaultWindowSize = new(1280, 720);
private static readonly Point DefaultWindowLocation = new(100, 100);
public string VideosDirectory { get; set; } = DEFAULT_VIDEO_DIR;
public string LabelsDirectory { get; set; } = DEFAULT_LABELS_DIR;
public string ImagesDirectory { get; set; } = DEFAULT_IMAGES_DIR;
public string ResultsDirectory { get; set; } = DEFAULT_RESULTS_DIR;
public string VideosDirectory { get; set; }
public string LabelsDirectory { get; set; }
public string ImagesDirectory { get; set; }
public string ResultsDirectory { get; set; }
public List<AnnotationClass> AnnotationClasses { get; set; } = [];
@@ -34,24 +23,44 @@ public class Config
public Size WindowSize { get; set; }
public Point WindowLocation { get; set; }
public bool ShowHelpOnStart { get; set; }
}
public void Save()
public interface IConfigRepository
{
public Config Get();
public void Save(Config config);
}
public class FileConfigRepository(ILogger<FileConfigRepository> logger) : IConfigRepository
{
private const string CONFIG_PATH = "config.json";
private const string DEFAULT_VIDEO_DIR = "video";
private const string DEFAULT_LABELS_DIR = "labels";
private const string DEFAULT_IMAGES_DIR = "images";
private const string DEFAULT_RESULTS_DIR = "results";
private static readonly Size DefaultWindowSize = new(1280, 720);
private static readonly Point DefaultWindowLocation = new(100, 100);
public Config Get()
{
File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(this, Formatting.Indented), Encoding.UTF8);
}
public static Config Read()
{
string configFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CONFIG_PATH);
var exePath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)!;
var configFilePath = Path.Combine(exePath, CONFIG_PATH);
logger.LogInformation($"exePath: {exePath}" );
logger.LogInformation($"configFilePath: {configFilePath}");
if (!File.Exists(configFilePath))
{
var exePath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)!;
return new Config
{
VideosDirectory = Path.Combine(exePath, DEFAULT_VIDEO_DIR),
LabelsDirectory = Path.Combine(exePath, DEFAULT_LABELS_DIR),
ImagesDirectory = Path.Combine(exePath, DEFAULT_IMAGES_DIR),
ResultsDirectory = Path.Combine(exePath, DEFAULT_RESULTS_DIR),
WindowLocation = DefaultWindowLocation,
WindowSize = DefaultWindowSize,
ShowHelpOnStart = true
@@ -67,4 +76,9 @@ public class Config
return new Config();
}
}
}
public void Save(Config config)
{
File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(config, Formatting.Indented), Encoding.UTF8);
}
}
+6 -4
View File
@@ -22,7 +22,7 @@ public partial class MainWindow
private readonly IMediator _mediator;
private readonly FormState _formState;
private readonly Config _config;
private readonly IConfigRepository _configRepository;
private readonly HelpWindow _helpWindow;
public ObservableCollection<AnnotationClass> AnnotationClasses { get; set; } = new();
@@ -30,6 +30,7 @@ public partial class MainWindow
private readonly TimeSpan _thresholdBefore = TimeSpan.FromMilliseconds(100);
private readonly TimeSpan _thresholdAfter = TimeSpan.FromMilliseconds(300);
private readonly Config _config;
public Dictionary<TimeSpan, List<YoloLabel>> AnnotationsDict { get; set; } = new();
private IntervalTree<TimeSpan, List<YoloLabel>> Annotations { get; set; } = new();
@@ -37,7 +38,7 @@ public partial class MainWindow
public MainWindow(LibVLC libVLC, MediaPlayer mediaPlayer,
IMediator mediator,
FormState formState,
Config config,
IConfigRepository configRepository,
HelpWindow helpWindow)
{
InitializeComponent();
@@ -45,7 +46,8 @@ public partial class MainWindow
_mediaPlayer = mediaPlayer;
_mediator = mediator;
_formState = formState;
_config = config;
_configRepository = configRepository;
_config = _configRepository.Get();
_helpWindow = helpWindow;
VideoView.Loaded += VideoView_Loaded;
@@ -232,7 +234,7 @@ public partial class MainWindow
_mediaPlayer.Dispose();
_libVLC.Dispose();
_config.AnnotationClasses = AnnotationClasses.ToList();
_config.Save();
_configRepository.Save(_config);
Application.Current.Shutdown();
}