简体   繁体   中英

c# Read real time from windows event log

i can succesfully read events from event log. But polling all events has very bad performance. I wonder if there is an event or something that i can subscribe to catch log entries "as they happen"?

Is this possible?

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.UserName,
            x.Message
        }).ToList();
        Console.WriteLine(entries[0].UserName);

You can use EventLogWatcher for this purpose. You can subscribe to desired log filter(s) and implement a handler function to execute when you receive any events.

    public static void eventLogSubscription()
    {

        using (EventLog eventLog = new EventLog("Application"))
        {
            String path = Path.GetTempPath();
            eventLog.Source = "Event Log Reader Application";
            eventLog.WriteEvent(new EventInstance(1003, 0, EventLogEntryType.Information), new object[] { "The event log watcher has started" , path});
            //eventLog.WriteEntry(arg.EventRecord.ToXml(), EventLogEntryType.Information, 1001, 1);
            eventLog.Dispose();
        }
        EventLogWatcher watcher = null;
        try
        {
            string eventQueryString = "*[System/EventID=4688]" +
                                           "and " +
                                           "*[EventData[Data[@Name = 'NewProcessName'] = 'C:\\Windows\\explorer.exe']]"; 
        
            EventLogQuery eventQuery = new EventLogQuery(
                "Security", PathType.LogName, eventQueryString);

            watcher = new EventLogWatcher(eventQuery);
            watcher.EventRecordWritten +=
                new EventHandler<EventRecordWrittenEventArgs>(
                    handlerExplorerLaunch);
            watcher.Enabled = true;
     
        }
        catch (EventLogReadingException e)
        {
            Console.WriteLine("Error reading the log: {0}", e.Message);
        }
        Console.ReadKey();
    }

    public static void handlerExplorerLaunch(object obj,
        EventRecordWrittenEventArgs arg)
    {            if (arg.EventRecord != null)
        {
            
            using (EventLog eventLog = new EventLog("Application"))
            {
                eventLog.Source = "Event Log Reader Application";
                eventLog.WriteEvent(new EventInstance(1001, 0, EventLogEntryType.Information), new object[] {arg.EventRecord.FormatDescription() });
                //eventLog.WriteEntry(arg.EventRecord.ToXml(), EventLogEntryType.Information, 1001, 1);
                eventLog.Dispose();
            }
        }
        else
        {
            Console.WriteLine("The event instance was null.");

        }
    }

I have found this to work more reliably.

using System;
using System.Diagnostics.Eventing.Reader;

static void Main(string[] args)
{
    if (args is null) throw new ArgumentNullException(nameof(args));

    LoadEventLogs();

    Console.ReadKey();
}

private static void LoadEventLogs()
{
    EventLogSession session = new EventLogSession();

    EventLogQuery query = new EventLogQuery("Security", PathType.LogName, "*[System/EventID=4688]")
    {
        TolerateQueryErrors = true,
        Session = session
    };

    EventLogWatcher logWatcher = new EventLogWatcher(query);

    logWatcher.EventRecordWritten += new EventHandler<EventRecordWrittenEventArgs>(LogWatcher_EventRecordWritten);

    try
    {
        logWatcher.Enabled = true;
    }
    catch (EventLogException ex)
    {
        Console.WriteLine(ex.Message);
        Console.ReadLine();
    }
}

private static void LogWatcher_EventRecordWritten(object sender, EventRecordWrittenEventArgs e)
{
    var time = e.EventRecord.TimeCreated;
    var id = e.EventRecord.Id;
    var logname = e.EventRecord.LogName;
    var level = e.EventRecord.Level;
    var task = e.EventRecord.TaskDisplayName;
    var opCode = e.EventRecord.OpcodeDisplayName;
    var mname = e.EventRecord.MachineName;

    Console.WriteLine($@"{time}, {id}, {logname}, {level}, {task}, {opCode}, {mname}");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM