Serilog
This commit is contained in:
@ -39,10 +39,6 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
|
||||
<PackageReference Include="MetadataExtractor" Version="2.8.0" />
|
||||
<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,4 +1,5 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Phares.Shared;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
@ -13,15 +14,15 @@ namespace View_by_Distance.Date.Group;
|
||||
public class DateGroup
|
||||
{
|
||||
|
||||
private readonly Serilog.ILogger? _Log;
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly List<string> _Exceptions;
|
||||
private readonly ILogger<Program>? _Logger;
|
||||
private readonly IsEnvironment _IsEnvironment;
|
||||
private readonly Models.Configuration _Configuration;
|
||||
private readonly List<KeyValuePair<string, string>> _FileKeyValuePairs;
|
||||
private readonly Dictionary<string, List<Tuple<string, Shared.Models.Property>>> _FilePropertiesKeyValuePairs;
|
||||
|
||||
public DateGroup(List<string> args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
||||
public DateGroup(List<string> args, ILogger<Program>? logger, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
||||
{
|
||||
if (isSilent)
|
||||
{ }
|
||||
@ -29,15 +30,15 @@ public class DateGroup
|
||||
{ }
|
||||
if (console is null)
|
||||
{ }
|
||||
_Logger = logger;
|
||||
_AppSettings = appSettings;
|
||||
_IsEnvironment = isEnvironment;
|
||||
_Exceptions = new List<string>();
|
||||
_Log = Serilog.Log.ForContext<DateGroup>();
|
||||
_FileKeyValuePairs = new List<KeyValuePair<string, string>>();
|
||||
_FilePropertiesKeyValuePairs = new Dictionary<string, List<Tuple<string, Shared.Models.Property>>>();
|
||||
Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
||||
Models.Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
||||
_Log.Information(propertyConfiguration.RootDirectory);
|
||||
_Logger?.LogInformation(propertyConfiguration.RootDirectory);
|
||||
Property.Models.Configuration.Verify(propertyConfiguration, requireExist: true);
|
||||
Verify(configuration);
|
||||
bool reverse = false;
|
||||
@ -49,12 +50,8 @@ public class DateGroup
|
||||
string[] dbFiles = Directory.GetFiles(propertyConfiguration.RootDirectory, "*.db", SearchOption.AllDirectories);
|
||||
foreach (string dbFile in dbFiles)
|
||||
File.Delete(dbFile);
|
||||
if (true || appSettings.MaxDegreeOfParallelism < 2)
|
||||
ticks = LogDelta(ticks, nameof(File.Delete));
|
||||
for (int i = 1; i < 10; i++)
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
|
||||
if (true || appSettings.MaxDegreeOfParallelism < 2)
|
||||
ticks = LogDelta(ticks, nameof(Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories));
|
||||
string destinationRoot = Property.Models.Stateless.IResult.GetResultsGroupDirectory(propertyConfiguration, "Z) Moved");
|
||||
string aResultsFullGroupDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(_Configuration.PropertyConfiguration, nameof(A_Property));
|
||||
string aPropertySingletonDirectory = Path.Combine(aResultsFullGroupDirectory, "{}");
|
||||
@ -67,8 +64,6 @@ public class DateGroup
|
||||
if (propertyConfiguration.PopulatePropertyId && (configuration.ByCreateDateShortcut || configuration.ByHash) && Shared.Models.Stateless.Methods.IProperty.Any(containers))
|
||||
{
|
||||
propertyLogic.SavePropertyParallelWork(ticks, metadata, t, containers);
|
||||
if (appSettings.MaxDegreeOfParallelism < 2)
|
||||
ticks = LogDelta(ticks, nameof(A_Property.SavePropertyParallelWork));
|
||||
if (propertyLogic.ExceptionsDirectories.Any())
|
||||
throw new Exception();
|
||||
}
|
||||
@ -133,17 +128,6 @@ public class DateGroup
|
||||
return result;
|
||||
}
|
||||
|
||||
private long LogDelta(long ticks, string? methodName)
|
||||
{
|
||||
long result;
|
||||
if (_Log is null)
|
||||
throw new NullReferenceException(nameof(_Log));
|
||||
double delta = new TimeSpan(DateTime.Now.Ticks - ticks).TotalMilliseconds;
|
||||
_Log.Debug($"{methodName} took {Math.Floor(delta)} millisecond(s)");
|
||||
result = DateTime.Now.Ticks;
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<(Item Item, long LastWriteTimeTicks, long MinimumDateTimeTicks, string[] Destination)> GetMoveFileCollection(string destinationDirectory, string topDirectory, Item[] filteredItems)
|
||||
{
|
||||
List<(Item Item, long LastWriteTimeTicks, long MinimumDateTimeTicks, string[] Destination)> results = new();
|
||||
@ -376,8 +360,6 @@ public class DateGroup
|
||||
|
||||
private void MoveFiles(Property.Models.Configuration configuration, string destinationRoot, Container[] containers)
|
||||
{
|
||||
if (_Log is null)
|
||||
throw new NullReferenceException(nameof(_Log));
|
||||
string checkDirectory;
|
||||
bool hasDuplicate;
|
||||
string fullFileName;
|
||||
@ -410,14 +392,14 @@ public class DateGroup
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
}
|
||||
}
|
||||
_Log.Information($"Ready to move {fileMoveCollectionAll.Length} file(s)?");
|
||||
_Logger?.LogInformation($"Ready to move {fileMoveCollectionAll.Length} 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(". . .");
|
||||
int moved = 0;
|
||||
foreach ((Item item, long lastWriteTimeTicks, long minimumDateTimeTicks, string[] destination) in fileMoveCollectionAll)
|
||||
{
|
||||
@ -454,14 +436,14 @@ public class DateGroup
|
||||
}
|
||||
}
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(destinationRoot);
|
||||
_Log.Information($"{moved} file(s) moved");
|
||||
_Logger?.LogInformation($"{moved} 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)
|
||||
break;
|
||||
}
|
||||
_Log.Information(". . .");
|
||||
_Logger?.LogInformation(". . .");
|
||||
foreach ((Item item, long lastWriteTimeTicks, long minimumDateTimeTicks, string[] destination) in fileMoveCollectionAll)
|
||||
{
|
||||
fullFileName = Path.Combine(destination);
|
||||
@ -472,7 +454,7 @@ public class DateGroup
|
||||
File.Move(fullFileName, item.ImageFileHolder.FullName);
|
||||
moved += 1;
|
||||
}
|
||||
_Log.Information($"Done moving back {moved} file(s)");
|
||||
_Logger?.LogInformation($"Done moving back {moved} file(s)");
|
||||
for (int i = 1; i < 10; i++)
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(configuration.RootDirectory);
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
namespace View_by_Distance.Date.Group.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.Date.Group.Models;
|
||||
@ -11,9 +13,8 @@ namespace View_by_Distance.Date.Group;
|
||||
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);
|
||||
@ -79,31 +77,28 @@ public class Program
|
||||
throw new Exception("Chelsea");
|
||||
#nullable restore
|
||||
Shared.Models.Console console = new();
|
||||
DateGroup _ = new(args, isEnvironment, configurationRoot, appSettings, workingDirectory, silentIndex > -1, console);
|
||||
DateGroup _ = new(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