cs-codex-dist-tests/Framework/KubernetesWorkflow/DownloadedLog.cs

94 lines
2.5 KiB
C#
Raw Permalink Normal View History

using Logging;
2023-04-13 09:30:19 +00:00
namespace KubernetesWorkflow
2023-04-13 09:30:19 +00:00
{
2023-06-27 13:28:00 +00:00
public interface IDownloadedLog
2023-04-13 09:30:19 +00:00
{
2024-07-29 09:02:24 +00:00
string ContainerName { get; }
2024-07-25 08:10:11 +00:00
void IterateLines(Action<string> action, params string[] thatContain);
2024-03-20 10:11:59 +00:00
string[] GetLinesContaining(string expectedString);
string[] FindLinesThatContain(params string[] tags);
2024-07-26 07:14:46 +00:00
string GetFilepath();
void DeleteFile();
2023-04-13 09:30:19 +00:00
}
2023-09-14 13:40:15 +00:00
internal class DownloadedLog : IDownloadedLog
2023-04-13 09:30:19 +00:00
{
private readonly LogFile logFile;
2024-07-29 09:02:24 +00:00
internal DownloadedLog(WriteToFileLogHandler logHandler, string containerName)
2023-04-13 09:30:19 +00:00
{
logFile = logHandler.LogFile;
2024-07-29 09:02:24 +00:00
ContainerName = containerName;
2023-04-13 09:30:19 +00:00
}
2024-07-29 09:02:24 +00:00
public string ContainerName { get; }
2023-04-13 09:30:19 +00:00
2024-07-25 08:10:11 +00:00
public void IterateLines(Action<string> action, params string[] thatContain)
{
using var file = File.OpenRead(logFile.FullFilename);
using var streamReader = new StreamReader(file);
var line = streamReader.ReadLine();
while (line != null)
{
2024-07-25 08:10:11 +00:00
if (thatContain.All(line.Contains))
{
action(line);
}
line = streamReader.ReadLine();
}
}
2024-03-20 10:11:59 +00:00
public string[] GetLinesContaining(string expectedString)
2023-04-13 09:30:19 +00:00
{
using var file = File.OpenRead(logFile.FullFilename);
using var streamReader = new StreamReader(file);
2024-03-20 10:11:59 +00:00
var lines = new List<string>();
2023-04-13 09:30:19 +00:00
var line = streamReader.ReadLine();
while (line != null)
{
2024-03-20 10:11:59 +00:00
if (line.Contains(expectedString))
{
lines.Add(line);
}
2023-04-13 09:30:19 +00:00
line = streamReader.ReadLine();
}
2024-03-20 10:11:59 +00:00
return lines.ToArray(); ;
2023-04-13 09:30:19 +00:00
}
public string[] FindLinesThatContain(params string[] tags)
{
var result = new List<string>();
using var file = File.OpenRead(logFile.FullFilename);
using var streamReader = new StreamReader(file);
var line = streamReader.ReadLine();
while (line != null)
{
if (tags.All(line.Contains))
{
result.Add(line);
}
line = streamReader.ReadLine();
}
return result.ToArray();
}
2024-07-26 07:14:46 +00:00
public string GetFilepath()
{
return logFile.FullFilename;
}
public void DeleteFile()
{
File.Delete(logFile.FullFilename);
}
2023-04-13 09:30:19 +00:00
}
}