473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading the Windows Event Log

Hey

I am trying to read the Windows Event Logc. In fact, I am able to read the
Event Log. My problem is that I am reading and filtering a large log and it
takes a very very very very long time to complete. I am using the ordinary
technique for reading/writing from and to the Event Log. I am wondering if
there is a better way to speed things up. Below is an excerpt of the code I
am using (notice that I am filtering by Category and TimeGenerated; again
this works fine on small logs but is painfully sloooooooowwwww on large ones):

DateTime eventDate = DateTime.MinValue;
EventLog eventLog = new EventLog(logName, machine);

foreach(EventLogEntry logEntry in eventLog.Entries)
{
if(logEntry.Category == "Logon/Logoff" && logEntry.TimeGenerated > eventDate)
{
//print the values
Console.Write(Convert.ToString(logEntry.EntryType) + "\t" +
logEntry.TimeGenerated.ToString() + "\t" + logEntry.Category + "\t" +
logEntry.UserName + "\n");
}
}

Please help.

Thanks
KK

Apr 8 '06 #1
1 16699
I suppose you are connecting to a remote system, in this case you might
speed up the process considerably by using System.Management and WMI.
Here is a complete sample, but I suggest you consult MSDN and the platform
sdk docs to get an idea what is done at the WMI level.
using System;
using System.Management;
using System.IO;
class App {
[MTAThread]
private static void Main(string[] args)
{
// Beware! the account used to connect must have remote WMI privileges on
the remote server.

RunProcess M = new RunProcess("adminuser", "adminpwd", "remservername");
M.Run();
}
}
sealed class RunProcess
{
private ConnectionOptions co;
private ManagementScope scope;

public RunProcess(string ConnectionUser, string ConnectionPassword, string
Machine )
{
co = new ConnectionOptions();
co.Username = ConnectionUser;
co.Password = ConnectionPassword;
co.Impersonation = ImpersonationLevel.Impersonate;
scope = new ManagementScope(@"\\" + Machine + @"\root\cimv2", co);
scope.Connect();
}
public void Run()
{
string logFileName = "security";
// default blocksize = 1, larger value may increase network throughput
EnumerationOptions opt = new EnumerationOptions();
opt.BlockSize = 1000;
// Get only Logon/LogOff category from security log
SelectQuery query = new SelectQuery("select CategoryString,
TimeGenerated, User, Type from Win32_NtLogEvent where Logfile ='" +
logFileName + "' " + "and category = 2");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query, opt))
{
foreach (ManagementObject mo in searcher.Get()) {
string logInfo = String.Format("{0} - {1} - {2}", mo["Type"],
mo["CategoryString"], mo["User"]);
Console.WriteLine(logInfo);
}
}
}
}

Willy.

"hecsan07" <he******@hotmail.com> wrote in message
news:4B**********************************@microsof t.com...
| Hey
|
| I am trying to read the Windows Event Logc. In fact, I am able to read the
| Event Log. My problem is that I am reading and filtering a large log and
it
| takes a very very very very long time to complete. I am using the ordinary
| technique for reading/writing from and to the Event Log. I am wondering if
| there is a better way to speed things up. Below is an excerpt of the code
I
| am using (notice that I am filtering by Category and TimeGenerated; again
| this works fine on small logs but is painfully sloooooooowwwww on large
ones):
|
| DateTime eventDate = DateTime.MinValue;
| EventLog eventLog = new EventLog(logName, machine);
|
| foreach(EventLogEntry logEntry in eventLog.Entries)
| {
| if(logEntry.Category == "Logon/Logoff" && logEntry.TimeGenerated >
eventDate)
| {
| //print the values
| Console.Write(Convert.ToString(logEntry.EntryType) + "\t" +
| logEntry.TimeGenerated.ToString() + "\t" + logEntry.Category + "\t" +
| logEntry.UserName + "\n");
| }
| }
|
| Please help.
|
| Thanks
| KK
|
Apr 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Bear | last post by:
I would like to develop some tools to better understand/analyze windows event logs. What I've done is export the event log as a delimited file, then try to use awk or python to parse the info....
0
by: Simon Wallis | last post by:
Can someone explain what Windows Event Trace is? I'm investigating EIF and Logging Application Block. Both refer to WET but I don't know what it is and can't find detailed information on it. What...
4
by: Greg Smith | last post by:
I have an old application that analyzes the data in the event log on one of our servers. I would like to convert it to C#. Does anybody know of any examples of reading the event log on a remote...
2
by: Gas | last post by:
Hi, Can anyone tell me how can I log an Windows Event Log using C#? Gas
1
by: Charlie | last post by:
Hi: I would like to query the Windows Event log and display items in a DataGrid on a WinForm. Is there a class in the framework to retieve data from the Event log? Thanks, Charlie
2
by: Abra | last post by:
Hi, I would like to be able from my C# .NET application to send (programatically) different debug messages to an own directory in the standard Windows Event Viewer. Which .NET classes provide...
1
by: Sean | last post by:
Here is a code I found that notifies if an event has been generated. I still can't find anything that would actually grab the event and export it a file which is what I am trying to do #include...
9
by: Jack | last post by:
Here is a code I found that notifies if an event has been generated. I still can't find anything that would actually grab the event and export it a file which is what I am trying to do #include...
2
by: Zytan | last post by:
What's a Window event log? EventLog documentation basically assumes the reader knows what it is. It says the following: http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.