Serilog
This commit is contained in:
@ -37,10 +37,6 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
<PackageReference Include="Serilog" Version="2.12.0" />
|
||||
<PackageReference Include="ShellProgressBar" Version="5.2.0" />
|
||||
<PackageReference Include="WindowsShortcutFactory" Version="1.1.0" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
|
||||
|
@ -1,6 +1,6 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Phares.Shared;
|
||||
using Serilog;
|
||||
using ShellProgressBar;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
@ -14,7 +14,7 @@ namespace View_by_Distance.Duplicate.Search;
|
||||
public class DuplicateSearch
|
||||
{
|
||||
|
||||
public DuplicateSearch(List<string> args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
||||
public DuplicateSearch(List<string> args, ILogger<Program> logger, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
||||
{
|
||||
if (isSilent)
|
||||
{ }
|
||||
@ -22,9 +22,8 @@ public class DuplicateSearch
|
||||
{ }
|
||||
string searchPattern = "*";
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
ILogger? log = Log.ForContext<DuplicateSearch>();
|
||||
Configuration configuration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
||||
log.Information(configuration.RootDirectory);
|
||||
logger?.LogInformation(configuration.RootDirectory);
|
||||
if (appSettings.IndexOnly)
|
||||
WriteIndexData(ticks, configuration, searchPattern);
|
||||
else
|
||||
@ -38,9 +37,9 @@ public class DuplicateSearch
|
||||
Dictionary<int, List<MappingFromItem?>> idToCollection = GetIdToCollection(argZero, configuration, argZeroIsConfigurationRootDirectory, containers, destinationRoot, preloadIds);
|
||||
int duplicates = (from l in idToCollection where l.Value.Count > 1 select 1).Sum();
|
||||
if (duplicates == 0)
|
||||
log.Information($"Found {duplicates} duplicate file(s)");
|
||||
logger?.LogInformation($"Found {duplicates} duplicate file(s)");
|
||||
else
|
||||
QuestionMove(ticks, log, destinationRoot, idToCollection, duplicates);
|
||||
QuestionMove(ticks, logger, destinationRoot, idToCollection, duplicates);
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +60,7 @@ public class DuplicateSearch
|
||||
File.WriteAllText(Path.Combine(alongSideDirectory, $"{directoryName}-{ticks}.json"), json);
|
||||
}
|
||||
|
||||
private static void Move(ILogger log, long ticks, string destinationRoot, List<(FileHolder ImageFileHolder, string Destination)> collection)
|
||||
private static void Move(ILogger<Program>? logger, long ticks, string destinationRoot, List<(FileHolder ImageFileHolder, string Destination)> collection)
|
||||
{
|
||||
StringBuilder stringBuilder = new();
|
||||
foreach ((FileHolder fileHolder, string destination) in collection)
|
||||
@ -70,28 +69,28 @@ public class DuplicateSearch
|
||||
_ = stringBuilder.AppendLine(destination);
|
||||
}
|
||||
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(Path.Combine(destinationRoot, $"{ticks}file(s).lsv"), stringBuilder.ToString(), updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
log.Information($"Ready to move {collection.Count} file(s)?");
|
||||
logger?.LogInformation($"Ready to move {collection.Count} file(s)?");
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
log.Information("Press \"Y\" key to move file(s) or close console to not move files");
|
||||
logger?.LogInformation("Press \"Y\" key to move file(s) or close console to not move files");
|
||||
if (System.Console.ReadKey().Key == ConsoleKey.Y)
|
||||
break;
|
||||
}
|
||||
log.Information(". . .");
|
||||
logger?.LogInformation(". . .");
|
||||
foreach ((FileHolder fileHolder, string destination) in collection)
|
||||
{
|
||||
try
|
||||
{ File.Move(fileHolder.FullName, destination); }
|
||||
catch (Exception exception)
|
||||
{ log.Error(exception, $"Failed to move <{fileHolder.FullName}>"); }
|
||||
{ logger?.LogError(exception, $"Failed to move <{fileHolder.FullName}>"); }
|
||||
}
|
||||
log.Information($"{collection.Count} file(s) moved");
|
||||
logger?.LogInformation($"{collection.Count} file(s) moved");
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
log.Information("Press \"Y\" key to move file(s) back or close console to leave them moved");
|
||||
logger?.LogInformation("Press \"Y\" key to move file(s) back or close console to leave them moved");
|
||||
if (System.Console.ReadKey().Key != ConsoleKey.Y)
|
||||
continue;
|
||||
log.Information(". . .");
|
||||
logger?.LogInformation(". . .");
|
||||
foreach ((FileHolder fileHolder, string destination) in collection)
|
||||
{
|
||||
if (!File.Exists(destination))
|
||||
@ -101,27 +100,27 @@ public class DuplicateSearch
|
||||
try
|
||||
{ File.Move(destination, fileHolder.FullName); }
|
||||
catch (Exception exception)
|
||||
{ log.Error(exception, $"Failed to move <{destination}>"); }
|
||||
{ logger?.LogError(exception, $"Failed to move <{destination}>"); }
|
||||
}
|
||||
}
|
||||
log.Information(". . .");
|
||||
logger?.LogInformation(". . .");
|
||||
}
|
||||
|
||||
private static void QuestionMove(long ticks, ILogger log, string destinationRoot, Dictionary<int, List<MappingFromItem?>> idToCollection, int duplicates)
|
||||
private static void QuestionMove(long ticks, ILogger<Program>? logger, string destinationRoot, Dictionary<int, List<MappingFromItem?>> idToCollection, int duplicates)
|
||||
{
|
||||
int[] ids = (from l in idToCollection orderby l.Key where l.Value.Any(m => m is not null) select l.Key).ToArray();
|
||||
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(Path.Combine(destinationRoot, $"{ticks}-id(s).lsv"), string.Join(Environment.NewLine, ids), updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
string json = JsonSerializer.Serialize(idToCollection, new JsonSerializerOptions { WriteIndented = true });
|
||||
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(Path.Combine(destinationRoot, $"{ticks}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
log.Information($"Found {duplicates} duplicate file(s)");
|
||||
logger?.LogInformation($"Found {duplicates} duplicate file(s)");
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
log.Information("Press \"Y\" key to continue or close console to leave them moved");
|
||||
logger?.LogInformation("Press \"Y\" key to continue or close console to leave them moved");
|
||||
if (System.Console.ReadKey().Key != ConsoleKey.Y)
|
||||
continue;
|
||||
log.Information(". . .");
|
||||
logger?.LogInformation(". . .");
|
||||
List<(FileHolder ImageFileHolder, string Destination)> collection = GetCollectionAndCreateDirectories(idToCollection);
|
||||
Move(log, ticks, destinationRoot, collection);
|
||||
Move(logger, ticks, destinationRoot, collection);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,10 +0,0 @@
|
||||
namespace View_by_Distance.Duplicate.Search.Models.Stateless;
|
||||
|
||||
public static class SerilogExtensionMethods
|
||||
{
|
||||
|
||||
internal static void Warn(this Serilog.ILogger log, string messageTemplate) => log.Warning(messageTemplate);
|
||||
|
||||
internal static void Info(this Serilog.ILogger log, string messageTemplate) => log.Information(messageTemplate);
|
||||
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Phares.Shared;
|
||||
using Serilog;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using View_by_Distance.Duplicate.Search.Models;
|
||||
@ -11,9 +13,8 @@ namespace View_by_Distance.Duplicate.Search;
|
||||
public class Program
|
||||
{
|
||||
|
||||
public static void Secondary(List<string> args)
|
||||
public static void Secondary(ILogger<Program> logger, List<string> args)
|
||||
{
|
||||
LoggerConfiguration loggerConfiguration = new();
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
|
||||
IsEnvironment isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
|
||||
@ -30,9 +31,6 @@ public class Program
|
||||
throw new Exception("Working directory name must have a value!");
|
||||
string workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
|
||||
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
|
||||
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
|
||||
Log.Logger = loggerConfiguration.CreateLogger();
|
||||
ILogger log = Log.ForContext<Program>();
|
||||
int silentIndex = args.IndexOf("s");
|
||||
if (silentIndex > -1)
|
||||
args.RemoveAt(silentIndex);
|
||||
@ -41,31 +39,28 @@ public class Program
|
||||
if (args is null)
|
||||
throw new Exception("args is null!");
|
||||
Shared.Models.Console console = new();
|
||||
_ = new DuplicateSearch(args, isEnvironment, configurationRoot, appSettings, workingDirectory, silentIndex > -1, console);
|
||||
_ = new DuplicateSearch(args, logger, isEnvironment, configurationRoot, appSettings, workingDirectory, silentIndex > -1, console);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.Fatal(string.Concat(ex.Message, Environment.NewLine, ex.StackTrace));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
logger?.LogError(ex, "Error!");
|
||||
}
|
||||
if (silentIndex > -1)
|
||||
log.Debug("Done. Bye");
|
||||
logger?.LogDebug("Done. Bye");
|
||||
else
|
||||
{
|
||||
log.Debug("Done. Press 'Enter' to end");
|
||||
logger?.LogDebug("Done. Press 'Enter' to end");
|
||||
_ = Console.ReadLine();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
ILogger<Program>? logger = Host.CreateDefaultBuilder(args).Build().Services.GetRequiredService<ILogger<Program>>();
|
||||
if (args is not null)
|
||||
Secondary(args.ToList());
|
||||
Secondary(logger, args.ToList());
|
||||
else
|
||||
Secondary(new List<string>());
|
||||
Secondary(logger, new List<string>());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user