/ Framework / Logging / DownloadedLog.cs
DownloadedLog.cs
 1  namespace Logging
 2  {
 3      public interface IDownloadedLog
 4      {
 5          string SourceName { get; }
 6  
 7          void IterateLines(Action<string> action);
 8          void IterateLines(Action<string> action, params string[] thatContain);
 9          string[] GetLinesContaining(string expectedString);
10          string[] FindLinesThatContain(params string[] tags);
11          string GetFilepath();
12          void DeleteFile();
13      }
14  
15      public class DownloadedLog : IDownloadedLog
16      {
17          private readonly LogFile logFile;
18  
19          public DownloadedLog(string filepath, string sourceName)
20          {
21              logFile = new LogFile(filepath);
22              SourceName = sourceName;
23          }
24  
25          public DownloadedLog(LogFile logFile, string sourceName)
26          {
27              this.logFile = logFile;
28              SourceName = sourceName;
29          }
30  
31          public string SourceName { get; }
32  
33          public void IterateLines(Action<string> action)
34          {
35              using var file = File.OpenRead(logFile.Filename);
36              using var streamReader = new StreamReader(file);
37  
38              var line = streamReader.ReadLine();
39              while (line != null)
40              {
41                  action(line);
42                  line = streamReader.ReadLine();
43              }
44          }
45  
46          public void IterateLines(Action<string> action, params string[] thatContain)
47          {
48              IterateLines(line =>
49              {
50                  if (thatContain.All(line.Contains))
51                  {
52                      action(line);
53                  }
54              });
55          }
56  
57          public string[] GetLinesContaining(string expectedString)
58          {
59              return FindLinesThatContain([expectedString]);
60          }
61  
62          public string[] FindLinesThatContain(params string[] tags)
63          {
64              var result = new List<string>();
65              IterateLines(result.Add, tags);
66              return result.ToArray();
67          }
68  
69          public string GetFilepath()
70          {
71              return logFile.Filename;
72          }
73  
74          public void DeleteFile()
75          {
76              File.Delete(logFile.Filename);
77          }
78      }
79  }