Read event log in C#

后端 未结 5 2111
北海茫月
北海茫月 2020-12-14 17:25

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

This code re

相关标签:
5条回答
  • 2020-12-14 17:45

    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();
    
    0 讨论(0)
  • 2020-12-14 17:47

    MSDN (1)(2) says that Source is for writing event logs only.

    It is not necessary to specify a Source when only reading from a log. You can specify only the Log name and MachineName (server computer name) properties for the EventLog instance. In either case, the Entries member is automatically populated with the event log's list of entries. You can select the appropriate index for an item in this list to read individual entries. (1)

    0 讨论(0)
  • 2020-12-14 17:52

    If you connect to localhost set MachineName to "." Check if user has right to read from eventlog

    0 讨论(0)
  • 2020-12-14 17:55

    Check out this article on MSDN. You can't read event log entries by source. Only log name matters. Instead you can create separate event log for you application or filter entries by verifying Source property of each entry in foreach loop.

    0 讨论(0)
  • I am not really sure what you were trying to print on the console. If it is the message in each event log entry that you are trying to print, inside the foreach loop you should have this instead:

    Console.WriteLine(log.Message + "\n");
    
    0 讨论(0)
提交回复
热议问题