public void HandlesCallbackExceptionTest() { bool called = false; EventLogSubscription.OnEntryWritten callback = (e) => { called = true; throw new Exception("My Exception"); }; StringWriter stdout = new StringWriter(); StringWriter stderr = new StringWriter(); EventLogSubscription tailer = new EventLogSubscription(logName, callback, stdout, stderr); tailer.Start(); testLog.WriteEntry(Guid.NewGuid().ToString()); int count = 0; while (!called && count++ < 10) { Thread.Sleep(250); } Assert.True(count < 10); Assert.Contains("My Exception", stderr.ToString()); }
public void ReadFromEventLogTest() { string actualLogMessage = ""; bool called = false; EventLogSubscription.OnEntryWritten callback = (e) => { called = true; actualLogMessage = e.Message; }; EventLogSubscription tailer = new EventLogSubscription(logName, callback, null, null); tailer.Start(); string expectedLogMessage = Guid.NewGuid().ToString(); testLog.WriteEntry(expectedLogMessage); int count = 0; while (!called && count++ < 10) { Thread.Sleep(250); } Assert.True(count < 10); Assert.True(called); Assert.Equal(expectedLogMessage, actualLogMessage); }
/// <summary> /// Will subscribe the active user to the event log with the supplied ID number /// </summary> /// <param name="id"></param> /// <param name="returnUrl"></param> /// <returns></returns> public ActionResult FollowPost(int id, string returnUrl) { int count = Db.EventLogs.Where(l => l.Id == id).Count(); if (count > 0) { EventLogSubscription subscription = new EventLogSubscription() { LogId = id, UserId = CurrentUser.Id }; try { Db.EventLogSubscriptions.Add(subscription); Db.SaveChanges(); } catch (Exception ex) { LogErrorMessage(ex); } } Response.Redirect(returnUrl); return(View()); }
/// <summary> /// Will unsubscribe the active user from the event log with the supplied ID number /// </summary> /// <param name="id"></param> /// <param name="returnUrl"></param> /// <returns></returns> public ActionResult UnfollowPost(int id, string returnUrl) { try { EventLogSubscription subscription = Db.EventLogSubscriptions.Where(s => s.UserId == CurrentUser.Id).Where(s => s.LogId == id).FirstOrDefault(); if (subscription != null) { Db.Entry(subscription).State = System.Data.Entity.EntityState.Deleted; Db.SaveChanges(); } } catch (Exception ex) { LogErrorMessage(ex); } Response.Redirect(returnUrl); return(View()); }
public static void Main(string[] args) { if (args.Length != 3) { throw new ArgumentException("Usage: [tcp|udp] url port"); } var protocol = args[0]; var url = args[1]; var port = int.Parse(args[2]); IForwarderInterface forwarder; if (protocol == "udp") { forwarder = new UDPForwarder(url, port); } else if (protocol == "tcp") { forwarder = new TCPForwarder(url, port); } else { throw new ArgumentException("Protocol must be either 'tcp' or 'udp' got: {0}", protocol); } var stdout = Console.Out; var stderr = Console.Error; string[] logsToListenTo = new[] { "Application", "Security", "System" }; foreach (var logToListenTo in logsToListenTo) { var subscription = new EventLogSubscription(logToListenTo, forwarder.Write, stdout, stderr); subscription.Start(); } WriteAMessageToTheEventLog($"Connected Event Log Forwarding"); while (true) { Thread.Sleep(3600000); // 1 Hour } }
public void OnlyReadsNewEventsTest() { const string lastMessage = "last message"; bool called = false; List <string> msgs = new List <string>(); EventLogSubscription.OnEntryWritten callback = (e) => { msgs.Add(e.Message); if (e.Message == lastMessage) { called = true; } }; using (EventLogSubscription tailer = new EventLogSubscription(logName, callback, null, null)) { string oldLogMessage = Guid.NewGuid().ToString(); for (int i = 0; i < 5; i++) { testLog.WriteEntry(oldLogMessage); } Tailer tailer = new Tailer(logName, callback, null, null); tailer.Start(); testLog.WriteEntry(lastMessage); int count = 0; while (!called && count++ < 40) { Thread.Sleep(250); } Assert.True(count < 10); Assert.Equal(1, msgs.Count); } }