// (c) 2007 Richard Grimes // www.grimes.demon.co.uk using System; using System.Diagnostics; using System.Collections.Specialized; class App { const string sourceName = "Acme_Source"; const string logName = "TestLog"; static void Main(string[] args) { StringCollection sysLogs = new StringCollection(); sysLogs.AddRange(new string[] {"application", "system", "security"}); if (args.Length > 0) { if (EventLog.SourceExists(sourceName)) { string log = EventLog.LogNameFromSourceName(sourceName, "."); EventLog.DeleteEventSource(sourceName); if (!sysLogs.Contains(log.ToLower())) { EventLog.Delete(log); } } return; } if (!EventLog.SourceExists(sourceName)) { EventLog.CreateEventSource(sourceName, logName); Console.WriteLine("Event source created - restart application"); return; } using (EventLog el = new EventLog()) { el.Source = sourceName; el.WriteEntry("Application started", EventLogEntryType.Information, 0, 0); Random rand = new Random(Environment.TickCount); for (int x = 0; x < 1000; ++x) { if (rand.Next(1000) < 2) { el.WriteEntry("Important Message!", EventLogEntryType.Error, 2, 0); } else { el.WriteEntry("Trivial Message", EventLogEntryType.Information, 3, 0); } } el.WriteEntry("Application ended", EventLogEntryType.Information, 1, 0); } } }