System.Diagnostics.EventLog.- Code: Select all
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.IO;
class Program
{
static void Main()
{
try
{
// Получаем список доступных журналов
string[] logNames = EventLog.GetEventLogs()
.Select(l => l.Log)
.Distinct()
.ToArray();
Console.WriteLine("Доступные журналы событий Windows:");
for (int i = 0; i < logNames.Length; i++)
{
Console.WriteLine($"{i + 1}. {logNames[i]}");
}
// Выбор журнала
Console.Write("\nВведите номер журнала: ");
if (!int.TryParse(Console.ReadLine(), out int choice) ||
choice < 1 || choice > logNames.Length)
{
Console.WriteLine("Некорректный выбор.");
return;
}
string selectedLog = logNames[choice - 1];
// Фильтр по дате
Console.Write("За сколько дней назад выводить события? (0 = все): ");
if (!int.TryParse(Console.ReadLine(), out int days) || days < 0)
{
Console.WriteLine("Некорректное число.");
return;
}
DateTime minDate = days > 0 ? DateTime.Now.AddDays(-days) : DateTime.MinValue;
Console.WriteLine($"\nЧтение журнала: {selectedLog}, события с {minDate}");
EventLog eventLog = new EventLog(selectedLog);
// Генерация HTML
StringBuilder html = new StringBuilder();
html.AppendLine("<!DOCTYPE html>");
html.AppendLine("<html><head><meta charset='UTF-8'>");
html.AppendLine("<title>Отчёт журнала событий Windows</title>");
html.AppendLine("<style>");
html.AppendLine("body { font-family: Arial; }");
html.AppendLine("table { border-collapse: collapse; width: 100%; }");
html.AppendLine("th, td { border: 1px solid #ccc; padding: 8px; }");
html.AppendLine("th { background-color: #f2f2f2; }");
html.AppendLine("tr:nth-child(even) { background-color: #f9f9f9; }");
html.AppendLine("</style></head><body>");
html.AppendLine($"<h2>Отчёт журнала событий Windows: {selectedLog}</h2>");
html.AppendLine($"<p>События с {minDate}</p>");
html.AppendLine("<table>");
html.AppendLine("<tr><th>Дата</th><th>Источник</th><th>Тип</th><th>Сообщение</th></tr>");
foreach (EventLogEntry entry in eventLog.Entries)
{
if (entry.EntryType == EventLogEntryType.Error &&
entry.TimeGenerated >= minDate)
{
html.AppendLine("<tr>");
html.AppendLine($"<td>{entry.TimeGenerated}</td>");
html.AppendLine($"<td>{entry.Source}</td>");
html.AppendLine($"<td>{entry.EntryType}</td>");
html.AppendLine($"<td>{System.Net.WebUtility.HtmlEncode(entry.Message)}</td>");
html.AppendLine("</tr>");
}
}
html.AppendLine("</table>");
html.AppendLine("</body></html>");
// Сохраняем файл
string filePath = Path.Combine(Environment.CurrentDirectory, "EventLogReport.html");
File.WriteAllText(filePath, html.ToString(), Encoding.UTF8);
Console.WriteLine($"\nОтчёт сохранён в: {filePath}");
Console.WriteLine("Откройте файл в браузере для просмотра.");
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
}
}
}
Что делает этот код:
- Спрашивает журнал и дату.
- Собирает все ошибки за указанный период.
- Генерирует HTML-таблицу с красивым оформлением.
- Сохраняет файл EventLogReport.html в папку с программой.
- Кодирует спецсимволы, чтобы HTML не ломался.

