Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read event log in C#

Tags:

I'm trying to read event logs for my application EventLoggingApp. The problem is reading logs for my single source (EventLoggingApp).

This code read logs for every source. What is the problem? Any advice?

static void ReadEvenLog() {     string eventLogName = "Application";     string sourceName = "EventLoggingApp";     string machineName = "Tom";      EventLog eventLog = new EventLog();     eventLog.Log = eventLogName;     eventLog.Source = sourceName;     eventLog.MachineName = machineName;      foreach (EventLogEntry log in eventLog.Entries)     {         Console.WriteLine("{0}\n",log.Source);     } } 
like image 705
Tom159 Avatar asked Jun 30 '10 09:06

Tom159


People also ask

How do you read event logs?

Right click on the Start button and select Control Panel > System & Security and double-click Administrative tools. Double-click Event Viewer. Select the type of logs that you wish to review (ex: Application, System)

How do I view system diagnostics in EventLog?

You can do this when you start an application by opening the shortcut menu for the application (if you're using a mouse, right-click the application icon) and indicating that you want to run as an administrator. You can use EventLog to create custom event logs that you can view through the server's Event Viewer.

What is Evtquery?

Runs a query to retrieve events from a channel or log file that match the specified query criteria.

Where does EventLog WriteEntry write to?

The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.


1 Answers

Try this:

EventLog log = new EventLog("Security"); var entries = log.Entries.Cast<EventLogEntry>()                          .Where(x => x.InstanceId == 4624)                          .Select(x => new                          {                              x.MachineName,                              x.Site,                              x.Source,                              x.Message                          }).ToList(); 
like image 189
Domenico Avatar answered Nov 10 '22 06:11

Domenico