2
0
mirror of synced 2025-01-13 18:14:14 +00:00

94 lines
2.5 KiB
C#
Raw Permalink Normal View History

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