Serilog
This commit is contained in:
@ -38,10 +38,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 View_by_Distance.Delete.By.Distinct.Models;
|
||||
using View_by_Distance.Property.Models;
|
||||
@ -11,28 +11,27 @@ namespace View_by_Distance.Delete.By.Distinct;
|
||||
public class DeleteByDistinct
|
||||
{
|
||||
|
||||
public DeleteByDistinct(List<string> args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
||||
public DeleteByDistinct(List<string> args, ILogger<Program> logger, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
||||
{
|
||||
if (isSilent)
|
||||
{ }
|
||||
if (console is null)
|
||||
{ }
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
ILogger? log = Log.ForContext<DeleteByDistinct>();
|
||||
Dictionary<string, List<string>> keyValuePairs = new();
|
||||
Configuration configuration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
||||
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
||||
log.Information(configuration.RootDirectory);
|
||||
logger?.LogInformation(configuration.RootDirectory);
|
||||
if (!appSettings.TicksForLong && !appSettings.SizeForLong)
|
||||
throw new Exception("Check appSettings file!");
|
||||
bool compareIsPopulatedAndNotTheSame = !string.IsNullOrEmpty(appSettings.CompareRootDirectory) && appSettings.CompareRootDirectory != configuration.RootDirectory;
|
||||
Work(appSettings, ticks, log, configuration.RootDirectory, nameof(configuration.RootDirectory), options, keyValuePairs, logOnly: compareIsPopulatedAndNotTheSame);
|
||||
Work(appSettings, ticks, logger, configuration.RootDirectory, nameof(configuration.RootDirectory), options, keyValuePairs, logOnly: compareIsPopulatedAndNotTheSame);
|
||||
if (compareIsPopulatedAndNotTheSame)
|
||||
Work(appSettings, ticks, log, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, keyValuePairs, logOnly: false);
|
||||
Work(appSettings, ticks, logger, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, keyValuePairs, logOnly: false);
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(appSettings.CompareRootDirectory);
|
||||
}
|
||||
|
||||
private static void Work(AppSettings appSettings, long ticks, ILogger log, string directory, string variable, ProgressBarOptions options, Dictionary<string, List<string>> keyValuePairs, bool logOnly)
|
||||
private static void Work(AppSettings appSettings, long ticks, ILogger<Program>? logger, string directory, string variable, ProgressBarOptions options, Dictionary<string, List<string>> keyValuePairs, bool logOnly)
|
||||
{
|
||||
string check;
|
||||
string logFile;
|
||||
@ -48,7 +47,7 @@ public class DeleteByDistinct
|
||||
List<string> deletedDirectories = new();
|
||||
Dictionary<long, List<string>> longToCollectionB = new();
|
||||
List<(string Source, string Destination)> renameFiles = new();
|
||||
log.Information($"Gathering {appSettings.SearchPattern} files from <{directory}>");
|
||||
logger?.LogInformation($"Gathering {appSettings.SearchPattern} files from <{directory}>");
|
||||
(string directory, string[] files)[] leftCollection = Shared.Models.Stateless.Methods.IFileHolder.GetFiles(directory, appSettings.SearchPattern).ToArray();
|
||||
totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
||||
message = $") Looking for duplicates from <{directory}> - {totalSeconds} total second(s)";
|
||||
@ -105,21 +104,21 @@ public class DeleteByDistinct
|
||||
}
|
||||
}
|
||||
progressBar.Dispose();
|
||||
log.Information(". . .");
|
||||
logger?.LogInformation(". . .");
|
||||
logFile = $"{ticks}-{variable}-Files-A.lsv";
|
||||
if (!logOnly)
|
||||
File.WriteAllLines(Path.Combine(directory, logFile), deletedFiles);
|
||||
if (deletedFiles.Any() && !logOnly)
|
||||
{
|
||||
log.Information($"Ready to delete {deletedFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{logFile}>");
|
||||
logger?.LogInformation($"Ready to delete {deletedFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{logFile}>");
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
log.Information("Press \"Y\" key to delete file(s), \"N\" key to log file(s) or close console to not delete files");
|
||||
logger?.LogInformation("Press \"Y\" key to delete file(s), \"N\" key to log file(s) or close console to not delete files");
|
||||
consoleKey = Console.ReadKey().Key;
|
||||
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
|
||||
break;
|
||||
}
|
||||
log.Information(". . .");
|
||||
logger?.LogInformation(". . .");
|
||||
if (consoleKey is not null && consoleKey.Value == ConsoleKey.Y)
|
||||
{
|
||||
foreach (string file in deletedFiles)
|
||||
@ -150,7 +149,7 @@ public class DeleteByDistinct
|
||||
deletedDirectories.AddRange(collection);
|
||||
}
|
||||
progressBar.Dispose();
|
||||
log.Information(". . .");
|
||||
logger?.LogInformation(". . .");
|
||||
logFile = $"{ticks + 1}-{variable}-Directories.lsv";
|
||||
File.WriteAllLines(Path.Combine(directory, logFile), deletedDirectories.Distinct());
|
||||
}
|
||||
@ -161,15 +160,15 @@ public class DeleteByDistinct
|
||||
File.WriteAllLines(Path.Combine(directory, logFile), renameFiles.Select(l => l.Source));
|
||||
logFile = $"{ticks}-{variable}-Files-C.lsv";
|
||||
File.WriteAllLines(Path.Combine(directory, logFile), renameFiles.Select(l => l.Destination));
|
||||
log.Information($"Ready to rename to match {renameFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{logFile}>");
|
||||
logger?.LogInformation($"Ready to rename to match {renameFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{logFile}>");
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
log.Information("Press \"Y\" key to rename to match file(s), \"N\" key to log file(s) or close console to not rename to match files");
|
||||
logger?.LogInformation("Press \"Y\" key to rename to match file(s), \"N\" key to log file(s) or close console to not rename to match files");
|
||||
consoleKey = Console.ReadKey().Key;
|
||||
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
|
||||
break;
|
||||
}
|
||||
log.Information(". . .");
|
||||
logger?.LogInformation(". . .");
|
||||
if (consoleKey is not null && consoleKey.Value == ConsoleKey.Y)
|
||||
{
|
||||
foreach ((string source, string destination) in renameFiles)
|
||||
|
@ -1,10 +0,0 @@
|
||||
namespace View_by_Distance.Delete.By.Distinct.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.Delete.By.Distinct.Models;
|
||||
@ -11,9 +13,8 @@ namespace View_by_Distance.Delete.By.Distinct;
|
||||
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 DeleteByDistinct(args, isEnvironment, configurationRoot, appSettings, workingDirectory, silentIndex > -1, console);
|
||||
_ = new DeleteByDistinct(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