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
+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);
}
}