diff --git a/Cajetan.OF-DL/Extensions/LoggingExtensions.cs b/Cajetan.OF-DL/Extensions/LoggingExtensions.cs new file mode 100644 index 0000000..26f13a8 --- /dev/null +++ b/Cajetan.OF-DL/Extensions/LoggingExtensions.cs @@ -0,0 +1,43 @@ +using System.Diagnostics; + +namespace OF_DL.Extensions; + +internal static class LoggingExtensions +{ + extension(Log) + { + public static ILogger ForProcessContext(Process process) + => Log.Logger.ForProcessContext(process); + + public static ILogger ForProcessContext(string propertyName, Process process) + => Log.Logger.ForProcessContext(propertyName, process); + + public static ILogger ForProcessContext(Process[] processes) + => Log.Logger.ForProcessContext(processes); + } + + extension(ILogger logger) + { + public ILogger ForProcessContext(Process process) + => logger.ForProcessContext($"Process_{process.Id}", process); + + public ILogger ForProcessContext(string propertyName, Process process) + => logger.ForContext(propertyName, process.GenerateContext(), destructureObjects: true); + + public ILogger ForProcessContext(Process[] processes) + { + ILogger l = logger; + + foreach (Process p in processes) + l = l.ForProcessContext(p); + + return l; + } + } + + extension(Process p) + { + public object GenerateContext() + => new { p.Id, p.ProcessName }; + } +} diff --git a/Cajetan.OF-DL/ProgramCajetan.cs b/Cajetan.OF-DL/ProgramCajetan.cs index 7f26d76..7b124ee 100644 --- a/Cajetan.OF-DL/ProgramCajetan.cs +++ b/Cajetan.OF-DL/ProgramCajetan.cs @@ -1,5 +1,4 @@ using System.Diagnostics; -using System.Reflection; using Microsoft.Extensions.DependencyInjection; AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red)); @@ -10,7 +9,8 @@ static async Task RunAsync(string[] args) ServiceCollection services = await ConfigureServices(args); ServiceProvider serviceProvider = services.BuildServiceProvider(); - ExitIfOtherProcess(serviceProvider); + if (IsAlreadyRunning(serviceProvider)) + return; Worker worker = serviceProvider.GetRequiredService(); await worker.RunAsync(); @@ -162,23 +162,24 @@ static CajetanConfig ParseCommandlineArgs(string[] args, Config currentConfig) } } -static void ExitIfOtherProcess(ServiceProvider serviceProvider) +static bool IsAlreadyRunning(ServiceProvider serviceProvider) { - Assembly? entryAssembly = Assembly.GetEntryAssembly(); - AssemblyName? entryAssemblyName = entryAssembly?.GetName(); - - if (entryAssemblyName?.Name is null) - return; - Process thisProcess = Process.GetCurrentProcess(); - Process[] otherProcesses = [.. Process.GetProcessesByName(entryAssemblyName.Name).Where(p => p.Id != thisProcess.Id)]; + Process[] otherProcesses = [.. Process.GetProcessesByName(thisProcess.ProcessName).Where(p => p.Id != thisProcess.Id)]; if (otherProcesses.Length <= 0) - return; + { + Log.ForContext("AllProcesses", Process.GetProcesses().OrderBy(p => p.Id).Select(p => $"{p.Id}: {p.ProcessName}")) + .Information("All other processes found.."); + return false; + } AnsiConsole.Markup($"[green]Other OF DL process detected, exiting..\n[/]"); - Log.Warning("Other OF DL process detected, exiting.."); + + Log.ForProcessContext("ThisProcess", thisProcess) + .ForProcessContext(otherProcesses) + .Warning("Other OF DL process detected, exiting.."); serviceProvider.GetRequiredService().ExitWithCode(0); + return true; } - diff --git a/Cajetan.OF-DL/Properties/GlobalUsings.cs b/Cajetan.OF-DL/Properties/GlobalUsings.cs index 1e5f526..22c3b5c 100644 --- a/Cajetan.OF-DL/Properties/GlobalUsings.cs +++ b/Cajetan.OF-DL/Properties/GlobalUsings.cs @@ -3,6 +3,7 @@ global using OF_DL; global using OF_DL.CLI; global using OF_DL.Enumerations; global using OF_DL.Exceptions; +global using OF_DL.Extensions; global using OF_DL.Helpers; global using OF_DL.Models; global using OF_DL.Models.Config;