private void NotifyEventThread() { while (true) { _notifyResetEvent.WaitOne(); lock (this) { // after a clear, we something get notified // twice for the same entry if (_notifying) { return; } _notifying = true; } try { int oldest_entry = OldestEventLogEntry; if (_lastEntryWritten < oldest_entry) { _lastEntryWritten = oldest_entry; } int current_entry = _lastEntryWritten - oldest_entry; int last_entry = EntryCount + oldest_entry; for (int i = current_entry; i < (last_entry - 1); i++) { EventLogEntry entry = GetEntry(i); CoreEventLog.OnEntryWritten(entry); } _lastEntryWritten = last_entry; } finally { lock (this) _notifying = false; } } }
private void ReadEventLog(int index, byte [] buffer, ref int bytesRead, ref int minBufferNeeded) { const int max_retries = 3; // if the eventlog file changed since the handle was // obtained, then we need to re-try multiple times for (int i = 0; i < max_retries; i++) { int ret = PInvoke.ReadEventLog(ReadHandle, ReadFlags.Seek | ReadFlags.ForwardsRead, index, buffer, buffer.Length, ref bytesRead, ref minBufferNeeded); if (ret != 1) { int error = Marshal.GetLastWin32Error(); if (i < (max_retries - 1)) { CoreEventLog.Reset(); } else { throw new Win32Exception(error); } } } }
public override void Delete(string logName, string machineName) { using (RegistryKey eventLogKey = GetEventLogKey(machineName, true)) { if (eventLogKey == null) { throw new InvalidOperationException("The event log key does not exist."); } using (RegistryKey logKey = eventLogKey.OpenSubKey(logName, false)) { if (logKey == null) { throw new InvalidOperationException(string.Format( CultureInfo.InvariantCulture, "Event Log '{0}'" + " does not exist on computer '{1}'.", logName, machineName)); } // remove all eventlog entries for specified log CoreEventLog.Clear(); // remove file holding event log entries string file = (string)logKey.GetValue("File"); if (file != null) { try { File.Delete(file); } catch (Exception) { // .NET seems to ignore failures here } } } eventLogKey.DeleteSubKeyTree(logName); } }
public override void EnableNotification() { if (file_watcher == null) { string logDir = FindLogStore(CoreEventLog.Log); if (!Directory.Exists(logDir)) { Directory.CreateDirectory(logDir); } file_watcher = new FileSystemWatcher(); file_watcher.Path = logDir; file_watcher.Created += delegate(object o, FileSystemEventArgs e) { lock (this) { if (_notifying) { return; } _notifying = true; } // allow for file to be finished writing Thread.Sleep(100); // Process every new entry in one notification event. try { while (GetLatestIndex() > last_notification_index) { try { CoreEventLog.OnEntryWritten(GetEntry(last_notification_index++)); } catch (Exception ex) { // FIXME: find some proper way to output this error Debug.WriteLine(ex); } } } finally { lock (this) _notifying = false; } }; } last_notification_index = GetLatestIndex(); file_watcher.EnableRaisingEvents = true; }
private void NotifyEventThread(ManualResetEvent resetEvent) { if (resetEvent == null) { return; } while (true) { try { resetEvent.WaitOne(); } catch (ObjectDisposedException) { // Notifications have been disabled and event // has been closed but not yet nulled. End thread. break; } lock (_eventLock) { if (resetEvent != _notifyResetEvent) { // A new thread has started with another reset event instance // or DisableNotifications has been called, setting // _notifyResetEvent to null. In both cases end this thread. break; } if (_readHandle == IntPtr.Zero) { break; } int oldest_entry = OldestEventLogEntry; if (_lastEntryWritten < oldest_entry) { _lastEntryWritten = oldest_entry; } int current_entry = _lastEntryWritten - oldest_entry; int last_entry = EntryCount + oldest_entry; for (int i = current_entry; i < (last_entry - 1); i++) { EventLogEntry entry = GetEntry(i); CoreEventLog.OnEntryWritten(entry); } _lastEntryWritten = last_entry; } } }
public override void EnableNotification() { _notifyResetEvent = new ManualResetEvent(false); _lastEntryWritten = OldestEventLogEntry + EntryCount; if (PInvoke.NotifyChangeEventLog(ReadHandle, _notifyResetEvent.Handle) == 0) { throw new InvalidOperationException(string.Format( CultureInfo.InvariantCulture, "Unable to receive notifications" + " for log '{0}' on computer '{1}'.", CoreEventLog.GetLogName(), CoreEventLog.MachineName), new Win32Exception()); } _notifyThread = new Thread(new ThreadStart(NotifyEventThread)); _notifyThread.IsBackground = true; _notifyThread.Start(); }