Last active
April 20, 2023 00:49
-
-
Save cdprf/190326c9ecd3ac3a4bfb57fdd3a74473 to your computer and use it in GitHub Desktop.
eventlog watcher #Windows #eventlogs #notify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Diagnostics; | |
| using System.Diagnostics.Eventing.Reader; | |
| using System.Threading; | |
| using System.Windows.Forms; | |
| using System.Drawing; | |
| namespace AdministrativeEventWatcher | |
| { | |
| class Program | |
| { | |
| private static EventLogWatcher _watcher; | |
| private static readonly AutoResetEvent _signal = new AutoResetEvent(false); | |
| private static NotifyIcon _notifyIcon1; | |
| [STAThread] | |
| static void Main(string[] args) | |
| { | |
| try | |
| { | |
| Console.WriteLine("Administrative Events takip ediliyor..."); | |
| // Initialize the NotifyIcon | |
| _notifyIcon1 = new NotifyIcon() | |
| { | |
| Icon = SystemIcons.Information, | |
| Visible = true | |
| }; | |
| _notifyIcon1.BalloonTipClicked += NotifyIcon1_BalloonTipClicked; | |
| // Query string for filtering events with a level of warning or higher. | |
| string query = "*[System/Level <= 3]"; | |
| // Create an EventLogQuery object for the Application and Services Logs/Administrative channel. | |
| EventLogQuery eventLogQuery = new EventLogQuery("Application and Services Logs/Administrative", PathType.LogName, query); | |
| // Initialize the EventLogWatcher with the EventLogQuery. | |
| _watcher = new EventLogWatcher(eventLogQuery); | |
| _watcher.EventRecordWritten += Watcher_EventRecordWritten; | |
| _watcher.Enabled = true; | |
| // Wait for events to be received. | |
| _signal.WaitOne(); | |
| // Stop the watcher when done. | |
| _watcher.Dispose(); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine($"Hata: {ex.Message}"); | |
| } | |
| } | |
| private static void NotifyIcon1_BalloonTipClicked(object sender, EventArgs e) | |
| { | |
| // Open Event Viewer | |
| Process.Start("eventvwr.msc"); | |
| } | |
| private static void Watcher_EventRecordWritten(object sender, EventRecordWrittenEventArgs e) | |
| { | |
| if (e.EventRecord != null) | |
| { | |
| Console.WriteLine("------------------------------------------"); | |
| Console.WriteLine($"Event ID: {e.EventRecord.Id}"); | |
| Console.WriteLine($"Event Level: {e.EventRecord.LevelDisplayName}"); | |
| Console.WriteLine($"Event Time: {e.EventRecord.TimeCreated}"); | |
| Console.WriteLine($"Event Source: {e.EventRecord.ProviderName}"); | |
| Console.WriteLine($"Event Message: {e.EventRecord.FormatDescription()}"); | |
| _notifyIcon1.ShowBalloonTip(10000, "Yeni Uyarı", $"{e.EventRecord.LevelDisplayName}: {e.EventRecord.ProviderName} - {e.EventRecord.FormatDescription()}", ToolTipIcon.Warning); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"EventRecordWritten Hatası: {e.Exception.Message}"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment