HelperMetrologyFiles.SortAndDelete

HelperEventLog.ClearEventLogs
This commit is contained in:
2024-01-26 14:13:55 -07:00
parent 58c4b9e487
commit e0f591e987
11 changed files with 276 additions and 13 deletions

34
Helpers/HelperEventLog.cs Normal file
View File

@ -0,0 +1,34 @@
using File_Watcher.Models;
using System.Diagnostics;
namespace File_Watcher.Helpers;
internal static partial class HelperEventLog
{
internal static bool ClearEventLogs(AppSettings appSettings, ILogger<Worker> logger)
{
if (Directory.Exists(appSettings.WatchDirectory))
{
#pragma warning disable CA1416
using (EventLog eventLog = new("Security", Environment.MachineName))
eventLog.Clear();
logger.LogInformation("{logName} log cleared.", "Security");
foreach (EventLog eventLog in EventLog.GetEventLogs())
{
try
{
eventLog.Clear();
eventLog.Dispose();
logger.LogInformation("{logName} log cleared.", eventLog.LogDisplayName);
}
catch (Exception ex)
{ logger.LogInformation("Error: {logName} - {message}.", eventLog.LogDisplayName, ex.Message); }
}
#pragma warning restore CA1416
logger.LogCritical("{Company}", appSettings.Company);
}
return true;
}
}