80 lines
2.1 KiB
C#
Raw Normal View History

2025-01-16 10:15:02 +01:00
namespace Logging
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
{
2025-01-16 10:15:02 +01:00
string SourceName { get; }
2024-07-29 11:02:24 +02:00
void IterateLines(Action<string> action);
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
}
2025-01-16 10:15:02 +01:00
public class DownloadedLog : IDownloadedLog
2023-04-13 11:30:19 +02:00
{
private readonly LogFile logFile;
2025-01-16 10:15:02 +01:00
public DownloadedLog(string filepath, string sourceName)
{
logFile = new LogFile(filepath);
SourceName = sourceName;
}
public DownloadedLog(LogFile logFile, string sourceName)
2023-04-13 11:30:19 +02:00
{
2025-01-16 10:15:02 +01:00
this.logFile = logFile;
SourceName = sourceName;
2023-04-13 11:30:19 +02:00
}
2025-01-16 10:15:02 +01:00
public string SourceName { get; }
2023-04-13 11:30:19 +02:00
public void IterateLines(Action<string> action)
{
2025-01-16 10:15:02 +01:00
using var file = File.OpenRead(logFile.Filename);
using var streamReader = new StreamReader(file);
var line = streamReader.ReadLine();
while (line != null)
{
action(line);
line = streamReader.ReadLine();
}
}
public void IterateLines(Action<string> action, params string[] thatContain)
2023-04-13 11:30:19 +02:00
{
IterateLines(line =>
2023-04-13 11:30:19 +02:00
{
if (thatContain.All(line.Contains))
2024-03-20 11:11:59 +01:00
{
action(line);
2024-03-20 11:11:59 +01:00
}
});
}
2023-04-13 11:30:19 +02:00
public string[] GetLinesContaining(string expectedString)
{
return FindLinesThatContain([expectedString]);
2023-04-13 11:30:19 +02:00
}
public string[] FindLinesThatContain(params string[] tags)
{
var result = new List<string>();
IterateLines(result.Add, tags);
return result.ToArray();
}
2024-07-26 09:14:46 +02:00
public string GetFilepath()
{
2025-01-16 10:15:02 +01:00
return logFile.Filename;
2024-07-26 09:14:46 +02:00
}
public void DeleteFile()
{
2025-01-16 10:15:02 +01:00
File.Delete(logFile.Filename);
}
2023-04-13 11:30:19 +02:00
}
}