mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 09:36:30 +00:00
use serilog along with microsoft logger, rework config handling
This commit is contained in:
@@ -8,6 +8,7 @@ using Azaion.Annotator.Extensions;
|
|||||||
using LibVLCSharp.Shared;
|
using LibVLCSharp.Shared;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||||
@@ -17,54 +18,63 @@ namespace Azaion.Annotator;
|
|||||||
|
|
||||||
public partial class App : Application
|
public partial class App : Application
|
||||||
{
|
{
|
||||||
private readonly ServiceProvider _serviceProvider;
|
private readonly IHost _host;
|
||||||
|
private readonly ILogger<App> _logger;
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
|
||||||
public App()
|
public App()
|
||||||
{
|
{
|
||||||
Log.Logger = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
.Enrich.FromLogContext()
|
.Enrich.FromLogContext()
|
||||||
.MinimumLevel.Warning()
|
.MinimumLevel.Information()
|
||||||
.WriteTo.Console()
|
.WriteTo.Console()
|
||||||
.WriteTo.File(
|
.WriteTo.File(
|
||||||
path: "Logs/log.txt",
|
path: "Logs/log.txt",
|
||||||
rollingInterval: RollingInterval.Day)
|
rollingInterval: RollingInterval.Day)
|
||||||
.CreateLogger();
|
.CreateLogger();
|
||||||
|
|
||||||
var services = new ServiceCollection();
|
_host = Host.CreateDefaultBuilder()
|
||||||
services.AddSingleton<MainWindow>();
|
.ConfigureServices((context, services) =>
|
||||||
services.AddSingleton<HelpWindow>();
|
{
|
||||||
services.AddMediatR(c => c.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
|
services.AddSingleton<MainWindow>();
|
||||||
services.AddSingleton<LibVLC>(_ => new LibVLC());
|
services.AddSingleton<HelpWindow>();
|
||||||
services.AddSingleton<FormState>();
|
services.AddMediatR(c => c.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
|
||||||
services.AddSingleton<MediaPlayer>(sp =>
|
services.AddSingleton<LibVLC>(_ => new LibVLC());
|
||||||
{
|
services.AddSingleton<FormState>();
|
||||||
var libVLC = sp.GetRequiredService<LibVLC>();
|
services.AddSingleton<MediaPlayer>(sp =>
|
||||||
return new MediaPlayer(libVLC);
|
{
|
||||||
});
|
var libVLC = sp.GetRequiredService<LibVLC>();
|
||||||
services.AddSingleton<Config>(_ => Config.Read());
|
return new MediaPlayer(libVLC);
|
||||||
services.AddSingleton<PlayerControlHandler>();
|
});
|
||||||
_serviceProvider = services.BuildServiceProvider();
|
services.AddSingleton<IConfigRepository, FileConfigRepository>();
|
||||||
_mediator = _serviceProvider.GetService<IMediator>()!;
|
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;
|
DispatcherUnhandledException += OnDispatcherUnhandledException;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
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;
|
e.Handled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnStartup(StartupEventArgs e)
|
protected override void OnStartup(StartupEventArgs e)
|
||||||
{
|
{
|
||||||
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.KeyDownEvent, new RoutedEventHandler(GlobalClick));
|
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)
|
private void GlobalClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var args = (KeyEventArgs)e;
|
var args = (KeyEventArgs)e;
|
||||||
|
|
||||||
_mediator.Publish(new KeyEvent(sender, args));
|
_mediator.Publish(new KeyEvent(sender, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,11 +14,14 @@
|
|||||||
<PackageReference Include="LibVLCSharp" Version="3.8.2" />
|
<PackageReference Include="LibVLCSharp" Version="3.8.2" />
|
||||||
<PackageReference Include="LibVLCSharp.WPF" Version="3.8.2" />
|
<PackageReference Include="LibVLCSharp.WPF" Version="3.8.2" />
|
||||||
<PackageReference Include="MediatR" Version="12.2.0" />
|
<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.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="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="RangeTree" Version="3.0.1" />
|
<PackageReference Include="RangeTree" Version="3.0.1" />
|
||||||
<PackageReference Include="Serilog" Version="4.0.0" />
|
<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.Extensions.Logging" Version="8.0.0" />
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Size = System.Windows.Size;
|
using Size = System.Windows.Size;
|
||||||
using Point = System.Windows.Point;
|
using Point = System.Windows.Point;
|
||||||
@@ -9,22 +10,10 @@ namespace Azaion.Annotator.DTO;
|
|||||||
|
|
||||||
public class Config
|
public class Config
|
||||||
{
|
{
|
||||||
private const string CONFIG_PATH = "config.json";
|
public string VideosDirectory { get; set; }
|
||||||
|
public string LabelsDirectory { get; set; }
|
||||||
private const string DEFAULT_VIDEO_DIR = "video";
|
public string ImagesDirectory { get; set; }
|
||||||
|
public string ResultsDirectory { get; set; }
|
||||||
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 List<AnnotationClass> AnnotationClasses { get; set; } = [];
|
public List<AnnotationClass> AnnotationClasses { get; set; } = [];
|
||||||
|
|
||||||
@@ -34,24 +23,44 @@ public class Config
|
|||||||
public Size WindowSize { get; set; }
|
public Size WindowSize { get; set; }
|
||||||
public Point WindowLocation { get; set; }
|
public Point WindowLocation { get; set; }
|
||||||
public bool ShowHelpOnStart { 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);
|
var exePath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)!;
|
||||||
}
|
var configFilePath = Path.Combine(exePath, CONFIG_PATH);
|
||||||
|
|
||||||
public static Config Read()
|
logger.LogInformation($"exePath: {exePath}" );
|
||||||
{
|
logger.LogInformation($"configFilePath: {configFilePath}");
|
||||||
string configFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CONFIG_PATH);
|
|
||||||
|
|
||||||
if (!File.Exists(configFilePath))
|
if (!File.Exists(configFilePath))
|
||||||
{
|
{
|
||||||
var exePath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)!;
|
|
||||||
return new Config
|
return new Config
|
||||||
{
|
{
|
||||||
VideosDirectory = Path.Combine(exePath, DEFAULT_VIDEO_DIR),
|
VideosDirectory = Path.Combine(exePath, DEFAULT_VIDEO_DIR),
|
||||||
LabelsDirectory = Path.Combine(exePath, DEFAULT_LABELS_DIR),
|
LabelsDirectory = Path.Combine(exePath, DEFAULT_LABELS_DIR),
|
||||||
ImagesDirectory = Path.Combine(exePath, DEFAULT_IMAGES_DIR),
|
ImagesDirectory = Path.Combine(exePath, DEFAULT_IMAGES_DIR),
|
||||||
|
ResultsDirectory = Path.Combine(exePath, DEFAULT_RESULTS_DIR),
|
||||||
|
|
||||||
WindowLocation = DefaultWindowLocation,
|
WindowLocation = DefaultWindowLocation,
|
||||||
WindowSize = DefaultWindowSize,
|
WindowSize = DefaultWindowSize,
|
||||||
ShowHelpOnStart = true
|
ShowHelpOnStart = true
|
||||||
@@ -67,4 +76,9 @@ public class Config
|
|||||||
return new Config();
|
return new Config();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public void Save(Config config)
|
||||||
|
{
|
||||||
|
File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(config, Formatting.Indented), Encoding.UTF8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public partial class MainWindow
|
|||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
private readonly FormState _formState;
|
private readonly FormState _formState;
|
||||||
|
|
||||||
private readonly Config _config;
|
private readonly IConfigRepository _configRepository;
|
||||||
private readonly HelpWindow _helpWindow;
|
private readonly HelpWindow _helpWindow;
|
||||||
|
|
||||||
public ObservableCollection<AnnotationClass> AnnotationClasses { get; set; } = new();
|
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 _thresholdBefore = TimeSpan.FromMilliseconds(100);
|
||||||
private readonly TimeSpan _thresholdAfter = TimeSpan.FromMilliseconds(300);
|
private readonly TimeSpan _thresholdAfter = TimeSpan.FromMilliseconds(300);
|
||||||
|
private readonly Config _config;
|
||||||
|
|
||||||
public Dictionary<TimeSpan, List<YoloLabel>> AnnotationsDict { get; set; } = new();
|
public Dictionary<TimeSpan, List<YoloLabel>> AnnotationsDict { get; set; } = new();
|
||||||
private IntervalTree<TimeSpan, List<YoloLabel>> Annotations { 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,
|
public MainWindow(LibVLC libVLC, MediaPlayer mediaPlayer,
|
||||||
IMediator mediator,
|
IMediator mediator,
|
||||||
FormState formState,
|
FormState formState,
|
||||||
Config config,
|
IConfigRepository configRepository,
|
||||||
HelpWindow helpWindow)
|
HelpWindow helpWindow)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@@ -45,7 +46,8 @@ public partial class MainWindow
|
|||||||
_mediaPlayer = mediaPlayer;
|
_mediaPlayer = mediaPlayer;
|
||||||
_mediator = mediator;
|
_mediator = mediator;
|
||||||
_formState = formState;
|
_formState = formState;
|
||||||
_config = config;
|
_configRepository = configRepository;
|
||||||
|
_config = _configRepository.Get();
|
||||||
_helpWindow = helpWindow;
|
_helpWindow = helpWindow;
|
||||||
|
|
||||||
VideoView.Loaded += VideoView_Loaded;
|
VideoView.Loaded += VideoView_Loaded;
|
||||||
@@ -232,7 +234,7 @@ public partial class MainWindow
|
|||||||
_mediaPlayer.Dispose();
|
_mediaPlayer.Dispose();
|
||||||
_libVLC.Dispose();
|
_libVLC.Dispose();
|
||||||
_config.AnnotationClasses = AnnotationClasses.ToList();
|
_config.AnnotationClasses = AnnotationClasses.ToList();
|
||||||
_config.Save();
|
_configRepository.Save(_config);
|
||||||
Application.Current.Shutdown();
|
Application.Current.Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user