Приветствую всех, помогите пожалуйста решить вопрос.
Задача состоит в том, чтобы определить есть ли в лог файле ошибки, если они есть - оповестить об этом.
Лог файл выглядит следующим образом:
spoiler2022-06-09 09:08:54 * Backup part for the task "windows" has ended *
2022-06-09 09:08:54 * Number of backed up files: 190340. Backup part size: 5,32 GB. *
2022-06-09 09:08:54 ** Backup for the task "windows" has ended **
2022-06-09 09:08:54 ** Processed files: 190347. Backed up files: 190340. Created folders: 10. Backup size: 5,32 GB. **
2022-06-09 09:08:54 ** Number of errors: 8. Time elapsed: 0 hours, 33 minutes, 32 seconds. **
2022-06-09 09:08:54 Deleting the Volume Shadow Copy image...
2022-06-09 09:08:54 The Volume Shadow Copy image has been successfully deleted.
2022-06-09 09:08:54 * Backup part for the task "windows" has ended *
2022-06-09 09:08:54 * Number of backed up files: 250340. Backup part size: 8,32 GB. *
2022-06-09 09:08:54 ** Backup for the task "windows" has ended **
2022-06-09 09:08:54 ** Processed files: 190347. Backed up files: 190340. Created folders: 10. Backup size: 5,32 GB. **
2022-06-09 09:08:54 ** Number of errors: 0. Time elapsed: 1 hours, 33 minutes, 32 seconds. **
2022-06-09 09:08:54 Deleting the Volume Shadow Copy image...
2022-06-09 09:08:54 The Volume Shadow Copy image has been successfully deleted.
2022-06-09 08:35:22 Backing up the task "windows"
2022-06-09 08:35:22 Applying the parameters to the task "windows" (if any).
2022-06-09 08:35:22 Requesting the creation of a Volume Shadow Copy image for the source...
2022-06-09 08:35:23 The Volume Shadow Copy image has been successfully created.
2022-06-09 08:35:23 Starting the copy. The user running the task is: СИСТЕМА
2022-06-09 09:08:54 ** Backup for the task "windows" has ended **
2022-06-09 09:08:54 ** Processed files: 190347. Backed up files: 190440. Created folders: 10. Backup size: 5,32 GB. **
2022-06-09 09:08:54 ** Number of errors: 2. Time elapsed: 0 hours, 34 minutes, 12 seconds. **
Данный код, находит все строки в файле, которые имеют: "2022-06-09 09:08:54 ** Number of errors: N. Time elapsed: 1 hours, 33 minutes, 32 seconds. **"
Но, когда я пытаюсь извлечь из этих строк количество ошибок, у меня записывается только значение из первой найденной строки =(
В данном примере это цифра 8, как я могу проверить все найденные строки?
Буду очень вам благодарен.
string DateNow = DateTime.UtcNow.ToString("yyyy-MM-dd");
try
{
var lines = File.ReadLines("Log File " + DateNow + ".txt");
string result = string.Join("\n",
lines.Where(s => s.IndexOf("errors: ", StringComparison.InvariantCultureIgnoreCase) >= 0));
string Find1 = "errors: ";
string Find2 = ". Time";
int start = result.IndexOf(Find1) + Find1.Length;
int end = result.IndexOf(Find2, start);
string NumErros = result.Substring(start, end - start);
Console.WriteLine(NumErros);
int DefaultNum = 0;
int CurrentErrors = int.Parse(NumErros);
if (CurrentErrors > DefaultNum)
{
Console.WriteLine("Значение > 0, ошибка найдена");
}
else
{
Console.WriteLine("Значение < 0, ошибка не найдена");
}
}
catch
{
Console.WriteLine("Лог не найден");
}
finally
{
Console.WriteLine("Конец");
}
Console.ReadLine();