Created
May 8, 2013 02:43
-
-
Save prettycode/5537830 to your computer and use it in GitHub Desktop.
Monitor for new Event Log entries. Basic proof-of-concept C# console app.
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Security.Cryptography; | |
using System.Xml.Linq; | |
using System.Security; | |
using System.Text.RegularExpressions; | |
namespace prettycode.org | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("[1] for event log enumeration"); | |
Console.WriteLine("[2] for event log monitoring"); | |
Console.Write("Selection: "); | |
switch (Console.ReadKey().KeyChar) | |
{ | |
case (char)'1': | |
Console.Clear(); | |
Program.EnumerateEventLogs(); | |
Console.Read(); | |
break; | |
case (char)'2': | |
Console.Clear(); | |
Console.WriteLine("Waiting for new event log entries..."); | |
Program.WatchForNewEntries("Application"); | |
break; | |
} | |
} | |
public static void EnumerateEventLogs() | |
{ | |
foreach (var log in EventLog.GetEventLogs()) | |
{ | |
try | |
{ | |
Console.WriteLine(String.Format("Log name: \"{0}\" (display name: \"{1}\")", log.Log, log.LogDisplayName)); | |
} | |
catch (SecurityException ex) | |
{ | |
Console.WriteLine(String.Format("{0} accessing log name \"{1}\"", ex.GetType(), log.Log)); | |
} | |
} | |
} | |
public static void WatchForNewEntries(string logName) | |
{ | |
int messageIndentWidth = 4; | |
int consoleWidth = Console.BufferWidth; | |
int messageLineWidth = Console.BufferWidth - messageIndentWidth; | |
new EventLogMonitor(logName, (entry) => | |
{ | |
Console.WriteLine("Write to \"" + logName + "\" at time " + entry.TimeWritten.ToString() + ": "); | |
Console.WriteLine("Message: " + entry.Message.Substring(0, Console.BufferWidth - 9)); | |
Console.WriteLine(); | |
}); | |
while (true) | |
{ | |
Thread.Sleep(0); | |
} | |
} | |
} | |
public class EventLogMonitor | |
{ | |
private readonly EventLog eventLog; | |
public Action<EventLogEntry> HandleWrite { get; set; } | |
public EventLogMonitor(string logName, Action<EventLogEntry> handleWrite = null) | |
{ | |
if (logName == null) | |
{ | |
throw new ArgumentNullException("logName"); | |
} | |
if (handleWrite != null) | |
{ | |
this.HandleWrite = handleWrite; | |
} | |
this.eventLog = new EventLog(logName) | |
{ | |
EnableRaisingEvents = true | |
}; | |
this.eventLog.EntryWritten += new EntryWrittenEventHandler(this.OnEntryWritten); | |
} | |
private void OnEntryWritten(object source, EntryWrittenEventArgs e) | |
{ | |
if (this.HandleWrite == null) | |
{ | |
throw new InvalidOperationException | |
("Event log entry has been written but HandleWrite has not been defined."); | |
} | |
this.HandleWrite.Invoke(e.Entry); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment