Query remote event log using c#

Hi There,

I'm to query the event log on a remote machine. Specifically, I'm trying to query the (very large) System event log on a print server. I've used the Resource Kit tool "Dump Event Log" (dumpel.exe) in the past but I'd rather not rely on a separate program to generate the data.

I've got some code which queries the event log and returns the rows I want, sweet, however it takes far too long (50+ seconds) to run. Dumpel.exe only takes a few seconds so the method Im using cant be all that great.

The code Im using is attached. Apart from being slow, I prefer it than having an external app included in my project. If theres a way to embed an exe into my app that would be fine too (Im not sure there is tho Im still a beginner).

Maybe the code can be rewritten to run more efficiently or perhaps there's a whole different approach we can take here - I'm open to suggestions =)


ManagementScope scope;
            ManagementObjectSearcher searcher;
            ManagementObjectCollection queryCollection;
            ConnectionOptions options = new ConnectionOptions();
            string Server = "[print server]";
            string Username = "[Domain]\\[Admin account]";
            string Password = "[]";
            options.Username = Username;
            options.Password = Password;
            scope = new ManagementScope("\\\\" + Server + "\\root\\cimv2", options);
            SelectQuery query = new SelectQuery("select * from Win32_NTLogEvent where Logfile = 'System' and EventCode = 10 and SourceName = 'Print' and Type = 'information' and timegenerated >20081020 and timegenerated <20081025");
            searcher = new ManagementObjectSearcher(scope, query);
                queryCollection = searcher.Get();
            catch (Exception e)

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

That's right... the WMI method is a bit slow.    However, there is another technique using the .Net Framework's own EventLog class
However, it's not that much faster for large event logs...  it can retrieve about 50,000 entries per minute (not a scientific measurement).   The speed decreases exponentionally as the size of the log file increases

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.