출처 http://mainia.tistory.com/412
이벤트로그 쓰기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
static void Main(string[] args) { WriteEventLogEntry("This is an entry in the event log by daveoncsharp.com"); } private static void WriteEventLogEntry(string message) { // Create an instance of EventLog System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog(); // Check if the event source exists. If not create it. if (!System.Diagnostics.EventLog.SourceExists("TestApplication")) { // "Application" 이름이 로그 트리에 나오므로 잘 정의해야 한다. System.Diagnostics.EventLog.CreateEventSource("TestApplication", "Application"); } // Set the source name for writing log entries. eventLog.Source = "TestApplication"; // Create an event ID to add to the event log int eventID = 8; // Write an entry to the event log. eventLog.WriteEntry(message, System.Diagnostics.EventLogEntryType.Error, eventID); // Close the Event Log eventLog.Close(); } |
이벤트 로그 보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
static void Main(string[] args) { String machine = "."; // local machine Console.WriteLine("-------------------------------------------------------------n"); if (args.Length == 1) { if (args[0] == "application" || args[0] == "system" || args[0] == "security") { String log = args[0]; EventLog aLog = new EventLog(log, machine); EventLogEntry entry; EventLogEntryCollection entries = aLog.Entries; Stack < EventLogEntry > stack = new Stack < EventLogEntry > (); for (int i = 0; i < entries.Count; i++) { entry = entries[i]; stack.Push(entry); } entry = stack.Pop(); // only display the last record Console.WriteLine("[Index]t" + entry.Index + "n[EventID]t" + entry.InstanceId + "n[TimeWritten]t" + entry.TimeWritten + "n[MachineName]t" + entry.MachineName + "n[Source]t" + entry.Source + "n[UserName]t" + entry.UserName + "n[Message]t" + entry.Message + "n---------------------------------------------------n"); } else { Console.WriteLine("Usage:glog.exe system(application,security)n"); } } else { Console.WriteLine("Usage:glog.exe system(application,security)n"); } Console.WriteLine("end"); Console.ReadLine(); } |