log pattern
This commit is contained in:
Mike Phares 2023-08-06 23:00:38 -07:00
parent 79b9daedfa
commit e6a70130c7
13 changed files with 756 additions and 455 deletions

13
.vscode/settings.json vendored
View File

@ -11,13 +11,26 @@
},
"cSpell.words": [
"ASPNETCORE",
"BIRT",
"CHIL",
"DEAT",
"endianness",
"FAMC",
"FAMS",
"GIVN",
"HUSB",
"INDI",
"Infineon",
"Kanban",
"kanbn",
"NSFX",
"OBJE",
"onenote",
"pged",
"Phares",
"Serilog",
"SUBM",
"SURN",
"SYSLIB"
]
}

File diff suppressed because it is too large Load Diff

View File

@ -76,7 +76,7 @@ internal static class HelperHardcodedFileSearchAndSort
}
}
}
log.LogInformation(sourceDirectory);
log.LogInformation("{sourceDirectory}", sourceDirectory);
}
}

View File

@ -41,7 +41,7 @@ internal static class HelperLogMerge
continue;
segment1 = segments[1];
hour = int.Parse(sourceFileNameWithoutExtension.Substring(8, 2));
if (keyValuePairs[day].ContainsKey(hour))
if (keyValuePairs[day].TryGetValue(hour, out _))
continue;
keyValuePairs[day].Add(hour, File.ReadAllLines(sourceFile));
moveFiles.Add(sourceFile);

View File

@ -9,14 +9,14 @@ internal static class HelperRenameToOldMoveDeleteOldMerge
{
string checkDirectory = argsZero[0..^1];
if (!Directory.Exists(checkDirectory))
log.LogInformation(string.Concat("<", checkDirectory, "> doesn't exist!"));
log.LogInformation("<{checkDirectory}> doesn't exist!", checkDirectory);
else
{
string renameFile;
string destinationFile;
List<string> deleteFiles = new();
string[] moveFiles = Directory.GetFiles(argsZero, "*", SearchOption.TopDirectoryOnly);
log.LogInformation(string.Concat("<", moveFiles.Length, "> to move"));
log.LogInformation("<{moveFiles.Length}> to move", moveFiles.Length);
foreach (string moveFile in moveFiles)
{
destinationFile = string.Concat(checkDirectory, moveFile[argsZero.Length..]);
@ -30,7 +30,7 @@ internal static class HelperRenameToOldMoveDeleteOldMerge
deleteFiles.Add(renameFile);
}
}
log.LogInformation(string.Concat("<", deleteFiles.Count, "> to delete"));
log.LogInformation("<{deleteFiles.Count}> to delete", deleteFiles.Count);
foreach (string deleteFile in deleteFiles)
{
for (short i = 0; i < short.MaxValue; i++)

View File

@ -5,30 +5,33 @@ using System.Text.RegularExpressions;
namespace File_Folder_Helper.Helpers;
internal static class HelperZipFilesByDate
internal static partial class HelperZipFilesByDate
{
[GeneratedRegex("[a-zA-Z0-9]{1,}")]
private static partial Regex LowerAlphaAlphaAndNumber();
internal static bool ZipFilesByDate(ILogger log, string sourceDirectory, SearchOption searchOption = SearchOption.TopDirectoryOnly, string dayFormat = "")
{
bool result = false;
string key;
bool addFile;
string? zipPath;
string fileName;
string? zipPath;
FileInfo fileInfo;
string weekOfYear;
string[] segments;
string[] subFiles;
string weekOfYear;
FileInfo fileInfo;
string zipDirectory;
DateTime creationTime;
string? directoryName;
DateTime lastWriteTime;
DateTime nowDateTime = DateTime.Now;
Regex regex = new("[a-zA-Z0-9]{1,}");
DateTime dateTime = DateTime.MinValue;
DateTime firstEmail = new(2019, 3, 8);
CultureInfo cultureInfo = new("en-US");
Calendar calendar = cultureInfo.Calendar;
Regex regex = LowerAlphaAlphaAndNumber();
Dictionary<string, DateTime> weeks = new();
int ticksLength = nowDateTime.AddDays(-6).Ticks.ToString().Length;
for (int i = 0; i < int.MaxValue; i++)
@ -107,14 +110,12 @@ internal static class HelperZipFilesByDate
{
key = Path.Combine(zipDirectory, $"{element.Key}.zip");
if (File.Exists(key))
{
for (short i = 101; i < short.MaxValue; i++)
{
key = Path.Combine(zipDirectory, $"{element.Key}_{i}.zip");
if (!File.Exists(key))
break;
}
}
using ZipArchive zip = ZipFile.Open(key, ZipArchiveMode.Create);
foreach (string file in element.Value)
{
@ -122,11 +123,9 @@ internal static class HelperZipFilesByDate
File.Delete(file);
}
if (zipPath is not null && Directory.Exists(zipPath) && !string.IsNullOrEmpty(directoryName))
{
try
{ Directory.SetLastWriteTime(directoryName, DateTime.Now); }
catch (Exception) { }
}
}
subFiles = Directory.GetFiles(zipDirectory, "*.zip", SearchOption.TopDirectoryOnly);
foreach (string subFile in subFiles)
@ -136,7 +135,6 @@ internal static class HelperZipFilesByDate
if (segments.Length > 2)
fileName = string.Concat(segments[0], '_', segments[1], '_', segments[2]);
if (weeks.TryGetValue(fileName, out DateTime value))
{
try
{
if (!result)
@ -144,14 +142,11 @@ internal static class HelperZipFilesByDate
File.SetLastWriteTime(subFile, value);
}
catch (Exception) { }
}
}
if (topDirectory != sourceDirectory)
{
try
{ _ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(topDirectory); }
catch (Exception) { }
}
log.LogInformation("{topDirectory}", topDirectory);
}
return result;
@ -166,7 +161,6 @@ internal static class HelperZipFilesByDate
string? zipDirectory;
DateTimeOffset? dateTimeOffset;
foreach (string zipFile in zipFiles)
{
try
{
dateTimeOffset = null;
@ -240,7 +234,6 @@ internal static class HelperZipFilesByDate
{ File.Move(zipFile, checkFile); }
catch (Exception) { log.LogInformation("<{zipFile}> couldn't be moved!", zipFile); }
}
}
return result;
}

23
Models/Birth.cs Normal file
View File

@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace File_Folder_Helper.Models;
public record Birth(DateOnly? Date,
string? Note,
List<string> Continue)
{
public override string ToString()
{
string result = JsonSerializer.Serialize(this, BirthSourceGenerationContext.Default.Birth);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Birth))]
internal partial class BirthSourceGenerationContext : JsonSerializerContext
{
}

23
Models/Change.cs Normal file
View File

@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace File_Folder_Helper.Models;
public record Change(DateOnly? Date,
string? Note,
List<string> Continue)
{
public override string ToString()
{
string result = JsonSerializer.Serialize(this, ChangeSourceGenerationContext.Default.Change);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Change))]
internal partial class ChangeSourceGenerationContext : JsonSerializerContext
{
}

24
Models/Death.cs Normal file
View File

@ -0,0 +1,24 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace File_Folder_Helper.Models;
public record Death(bool? Is,
DateOnly? Date,
string? Note,
List<string> Continue)
{
public override string ToString()
{
string result = JsonSerializer.Serialize(this, DeathSourceGenerationContext.Default.Death);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Death))]
internal partial class DeathSourceGenerationContext : JsonSerializerContext
{
}

25
Models/Name.cs Normal file
View File

@ -0,0 +1,25 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace File_Folder_Helper.Models;
public record Name(string? ForwardSlashFull,
string? Given,
string? Sur,
string? Nick,
string? Suffix)
{
public override string ToString()
{
string result = JsonSerializer.Serialize(this, NameSourceGenerationContext.Default.Name);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Name))]
internal partial class NameSourceGenerationContext : JsonSerializerContext
{
}

34
Models/Person.cs Normal file
View File

@ -0,0 +1,34 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace File_Folder_Helper.Models;
public record Person(long Id,
Name? Name,
char? Sex,
string? UId,
Birth? Birth,
Death? Death,
Change? Change,
string[] Lines)
{
public override string ToString()
{
string result = JsonSerializer.Serialize(this, PersonSourceGenerationContext.Default.Person);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Person))]
internal partial class PersonSourceGenerationContext : JsonSerializerContext
{
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Dictionary<long, Person>))]
internal partial class PeopleSourceGenerationContext : JsonSerializerContext
{
}

View File

@ -22,7 +22,7 @@ internal class Program
_ = builder.Services.AddHostedService<Worker>();
using IHost host = builder.Build();
ILogger<Program> logger = host.Services.GetRequiredService<ILogger<Program>>();
logger.LogCritical(appSettings.Company);
logger.LogCritical("appSettings.Company", appSettings.Company);
host.Run();
}

View File

@ -176,7 +176,7 @@ public class Worker : BackgroundService
throw new Exception(_Args[0]);
}
catch (Exception ex)
{ _Logger.LogError(string.Concat(ex.Message, Environment.NewLine, ex.StackTrace)); }
{ _Logger.LogError("{Message}{NewLine}{StackTrace}", ex.Message, Environment.NewLine, ex.StackTrace); }
if (_IsSilent)
_Logger.LogInformation("Done. Bye");
else