Re: Is there a way to query Security Event Log with Filter in C#?



Hi !

You can try WMI query for this.
Example that filters event log by LogFile and TimeGenerated.

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace QueryEventLog {

class Program {
static void Main(string[] args) {
string SomeDateTime = "20070101000000.000000+000";
string Query = String.Format("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Application' AND TimeGenerated > '{0}'", SomeDateTime);
ManagementObjectSearcher mos = new ManagementObjectSearcher(Query);
object o;

foreach (ManagementObject mo in mos.Get()) {

Console.WriteLine("///////////////////////////////////////////////////////////////////////////");
foreach (PropertyData pd in mo.Properties) {
o = mo[pd.Name];
if (o != null) {
Console.WriteLine(String.Format("{0}: {1}", pd.Name, mo[pd.Name].ToString()));
}
}
}

Console.ReadLine();
}
}
}

Hope it helps.

Petar Repac



Pucca wrote:
Thank you Jani. I'm already using the eventLog class and processing each log entry and filtering them in my C# code (vs2005, .net2.0) and then place the filtered / qualified rows in to a dataset table.

The problem is this is taking a long time. It's taking 45 secornds just to read about 45k of entries(I get the entrycollection then use a logentry varible to read each one). Are there anyway to improve this?
.



Relevant Pages

  • Re: Is there a way to query Security Event Log with Filter in C#?
    ... ManagementObjectSearcher mos = new ManagementObjectSearcher; ... foreach ) ... Example that filters event log by LogFile and TimeGenerated. ... foreach (PropertyData pd in mo.Properties) { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: php batch uploading multiple files
    ... batch process also makes the filenames unique and writes their names to ... filters out thumbnails ... foreach ...
    (php.general)
  • Re: Performace problem with DataView.RowFilter
    ... Joe - Sahil is dead right on this - I had reset the filters a ... > I need to loop through the smaller table and filter a DataView based on ... > foreach ... > foreach (DataRowView drv in dv) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Performace problem with DataView.RowFilter
    ... Joe - Sahil is dead right on this - I had reset the filters a ... > I need to loop through the smaller table and filter a DataView based on ... > foreach ... > foreach (DataRowView drv in dv) ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Inline Pipeline Filter/lambda
    ... how about a different 1 character for in-line ... You can try this out using aliases. ... I also find the "foreach" a bit confusing because I thought the ...
    (microsoft.public.windows.server.scripting)

Loading