Add a ShowLog method to try and troubleshoot why the logs are not getting cleaned up.

This commit is contained in:
2025-04-02 21:19:42 -04:00
parent 0315575dda
commit f4e5e43351
2 changed files with 64 additions and 2 deletions

View File

@@ -173,6 +173,7 @@ namespace MarketData.Services
Console.WriteLine($"Adding Trace Listener :{currentWorkingDirectory+logFolder+"/"+strLogFile}");
Trace.Listeners.Add(new TextWriterTraceListener(currentWorkingDirectory+logFolder+"/"+strLogFile));
MDTrace.WriteLine($"Trace Listener added.");
Utility.ShowLogs(currentWorkingDirectory + logFolder);
return true;
}

View File

@@ -86,6 +86,7 @@ namespace MarketData.Utils
}
}
public static void ExpireLogs(String pathLogFiles,int expiryDays)
{
DateTime currentDate = DateTime.Now;
@@ -97,16 +98,76 @@ namespace MarketData.Utils
{
DateTime creationTime = File.GetCreationTime(logFile);
int age = Math.Abs(dateGenerator.DaysBetweenActual(currentDate, creationTime));
MDTrace.WriteLine($"[ExpireLogs ]{logFile} is {age} {(age>1?"days":"day")} old");
Console.WriteLine($"[ExpireLogs ]{logFile} is {age} {(age>1?"days":"day")} old");
if(age>=expiryDays)
{
File.Delete(logFile);
}
}
catch(Exception){;}
catch(Exception exception)
{
Console.WriteLine($"{exception.ToString()}");
}
}
}
public static void ShowLogs(String pathLogFiles)
{
try
{
DateTime currentDate = DateTime.Now;
DateGenerator dateGenerator = new DateGenerator();
String[] logFiles=Directory.GetFiles(pathLogFiles,"*.log");
MDTrace.WriteLine(LogLevel.DEBUG,$"[ShowLogs] Log folder : {pathLogFiles}");
MDTrace.WriteLine(LogLevel.DEBUG,$"[ShowLogs] Found {logFiles.Length} log files.");
foreach(String logFile in logFiles)
{
DateTime creationTime = File.GetCreationTime(logFile);
DateTime lastWriteTime = File.GetLastWriteTime(logFile);
FileAttributes attributes = File.GetAttributes(logFile);
int age = Math.Abs(dateGenerator.DaysBetweenActual(currentDate, creationTime));
StringBuilder sb = new StringBuilder();
sb.Append($"[ShowLogs]");
sb.Append($"Age:").Append(age).Append(" ");
sb.Append($"Created:").Append(creationTime.ToShortDateString()).Append(" ");
sb.Append($"LastWite:").Append(lastWriteTime.ToShortDateString()).Append(" ");
sb.Append(FileAttributesToString(attributes));
MDTrace.WriteLine(LogLevel.DEBUG,sb.ToString());
}
}
catch(Exception exception)
{
MDTrace.WriteLine(LogLevel.DEBUG,$"{exception.ToString()}");
}
}
private static String FileAttributesToString(FileAttributes attributes)
{
StringBuilder sb = new StringBuilder();
List<String> attributesList = new List<String>();
int value = (int)attributes;
if(value==0)attributesList.Add("None");
if(1==(value & (int)FileAttributes.ReadOnly))attributesList.Add("ReadOnly");
if(1==(value & (int)FileAttributes.System))attributesList.Add("System");
if(1==(value & (int)FileAttributes.Hidden))attributesList.Add("Hidden");
if(1==(value & (int)FileAttributes.Directory))attributesList.Add("Directory");
if(1==(value & (int)FileAttributes.Archive))attributesList.Add("Archive");
if(1==(value & (int)FileAttributes.Device))attributesList.Add("Device");
if(1==(value & (int)FileAttributes.Normal))attributesList.Add("Normal");
if(1==(value & (int)FileAttributes.Temporary))attributesList.Add("Temporary");
if(1==(value & (int)FileAttributes.SparseFile))attributesList.Add("SparseFile");
if(1==(value & (int)FileAttributes.ReparsePoint))attributesList.Add("ReparsePoint");
if(1==(value & (int)FileAttributes.Compressed))attributesList.Add("Compressed");
if(1==(value & (int)FileAttributes.Offline))attributesList.Add("Offline");
if(1==(value & (int)FileAttributes.NotContentIndexed))attributesList.Add("NotContentIndexed");
if(1==(value & (int)FileAttributes.Encrypted))attributesList.Add("Encrypted");
if(1==(value & (int)FileAttributes.IntegrityStream))attributesList.Add("IntegrityStream");
if(1==(value & (int)FileAttributes.NoScrubData))attributesList.Add("NoScrubData");
return Utility.ListToString(attributesList);
}
public static String Pad(string str, char filler, int length)
{
int stringLength = str.Length;