473,806 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi, I'm using vs2005 and .net 2.0. I currently prcoess each Security Log
entry one by one to extract those that fit the selection criteria. Is there
a function that I can use to query the entries with option of filtering for
certain event id and/or time period in C#?

Thanks.
Jan 2 '07 #1
6 10080
Hi Pucca,
I currently prcoess each Security Log
entry one by one to extract those that fit the selection criteria. Is
there
a function that I can use to query the entries with option of filtering
for
certain event id and/or time period in C#?
I'm afraid no such function/feature exists in .NET 2.0, but it shouldn't be
too difficult to write your own filtering when you loop through the entries
in the log using the Entries property. Here's a basic "how to" article on
different things you can do with event logs:

http://support.microsoft.com/kb/815314

Also, you might wish to enable notifications when new entries are written to
the event log, and then filter the events there (if applicable to your
solution). See the Receive Event Notifications section in the above article.

Thanks!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethi s.dystopia.fi
http://www.saunalahti.fi/janij/
Jan 2 '07 #2
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?
--
Thanks.
"Jani Järvinen [MVP]" wrote:
Hi Pucca,
I currently prcoess each Security Log
entry one by one to extract those that fit the selection criteria. Is
there
a function that I can use to query the entries with option of filtering
for
certain event id and/or time period in C#?

I'm afraid no such function/feature exists in .NET 2.0, but it shouldn't be
too difficult to write your own filtering when you loop through the entries
in the log using the Entries property. Here's a basic "how to" article on
different things you can do with event logs:

http://support.microsoft.com/kb/815314

Also, you might wish to enable notifications when new entries are written to
the event log, and then filter the events there (if applicable to your
solution). See the Receive Event Notifications section in the above article.

Thanks!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethi s.dystopia.fi
http://www.saunalahti.fi/janij/
Jan 4 '07 #3
Hi !

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

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Manageme nt;

namespace QueryEventLog {

class Program {
static void Main(string[] args) {
string SomeDateTime = "20070101000000 .000000+000";
string Query = String.Format(" SELECT * FROM Win32_NTLogEven t
WHERE Logfile = 'Application' AND TimeGenerated '{0}'", SomeDateTime);
ManagementObjec tSearcher mos = new ManagementObjec tSearcher(Query );
object o;

foreach (ManagementObje ct mo in mos.Get()) {

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

Console.ReadLin e();
}
}
}

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?
Jan 4 '07 #4
"Pucca" <Pu***@discussi ons.microsoft.c omwrote in message
news:78******** *************** ***********@mic rosoft.com...
The odd thing is that it works when I change the logfile = 'Application'.
Wieht Security it retrievs 0 entry. Why is that so? I did verify that I
have over 55k of entries in Security log in Event Viewer.
--
Thanks.
"Pucca" wrote:
>Thanks Peter. I tried it in my code but it's just exiting when it eaches the
statement mos.get(). Can you see what's wrong here? Also, where can I look
up syntax format and the properties names for the Security log? Thanks.

private void GetLog()
{
//string SomeDateTime = "20060101000000 .000000+000";
//string Query = String.Format(" SELECT * FROM Win32_NTLogEven t WHERE
Logfile = 'Security' AND TimeGenerated '{0}'", SomeDateTime);
string Query = String.Format(" SELECT * FROM Win32_NTLogEven t WHERE
Logfile = 'Security'");

object o;
string name;
try
{
ManagementObjec tSearcher mos = new ManagementObjec tSearcher(Query );
foreach (ManagementObje ct mo in mos.Get())
{
foreach (PropertyData pd in mo.Properties)
{
o = mo[pd.Name];
if (o != null)
{

//Console.WriteLi ne(String.Forma t("{0}: {1}", pd.Name,
mo[pd.Name].ToString()));
}
}
}
mos.Dispose();
}
catch (Exception e)
{
MessageBox.Show (e.Message);
}
}
--
Thanks.
"Petar Repac" wrote:
Hi !

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

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Manageme nt;

namespace QueryEventLog {

class Program {
static void Main(string[] args) {
string SomeDateTime = "20070101000000 .000000+000";
string Query = String.Format(" SELECT * FROM Win32_NTLogEven t
WHERE Logfile = 'Application' AND TimeGenerated '{0}'", SomeDateTime);
ManagementObjec tSearcher mos = new ManagementObjec tSearcher(Query );
object o;

foreach (ManagementObje ct mo in mos.Get()) {

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

Console.ReadLin e();
}
}
}

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?


Only administrators can read the security log!

Willy.

Jan 5 '07 #5
"Pucca" <Pu***@discussi ons.microsoft.c omwrote in message
news:9D******** *************** ***********@mic rosoft.com...
Thanks Willy. I am login as an administrator on my Win2k server. Is there
any other setting that I need to configure for an administrator? Thanks.
--

Should work, please post your code.

Willy.

Jan 6 '07 #6
Thanks Willy. I alredy posted the code in my previous posting. Please have
a look and let me know what you think. Thanks.

p.s. The same query I ran in the WMI CIM Studio, and it says "No instances
found".
--
Thanks.
"Willy Denoyette [MVP]" wrote:
"Pucca" <Pu***@discussi ons.microsoft.c omwrote in message
news:9D******** *************** ***********@mic rosoft.com...
Thanks Willy. I am login as an administrator on my Win2k server. Is there
any other setting that I need to configure for an administrator? Thanks.
--


Should work, please post your code.

Willy.

Jan 6 '07 #7

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

Similar topics

4
2620
by: DebbieG | last post by:
I have a form based on this query: SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr, OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, & ", " & & " " & AS Name, Students.LastNM, Students.FirstNM, Students.MI, Students.DOB, Students.GenderCD, Students.EthnicityCD, Students.EligibilityCD, Students.UBInitiative, Students.NCESSchID, Students.ProjEntryDT, Students.ProjReEntDT, Students.LastSerDT, Students.Reason,...
4
7751
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query, but I can't update record in query or in form. I believe the problem is due to the source query. In source query, there is a filter to show the incomplete record ("is null" in delivery date)], but I need to re-use the job no. if the job is...
2
1537
by: andrew007 | last post by:
I have a question about dataset rowfilter. I have a list of event table in a database. each event has start-date and end-date column. I grab these from db and save to dataset. And then I have to do rowfilter of dataset to filter based on user's input. Users can select the following as dropdown for filtering. - last 30 days - last 7 days - today - all pasts
5
3706
by: jjyconsulting | last post by:
Newbie needing some help. I have a tblParticipants. The fields include gender, education_level, income, occupation etc., I'm trying to create a form where a user can run a query from the form and just choose the appropriate criterias from the combo boxes to get the results. I also want the query to run even if there is not a value in all the combo boxes ie., i want just all males with income level of over $100,000...Any insights or help...
1
1967
by: TF | last post by:
This group came through for me last time so here we go again. My page shows paint colors, brand name, product code, etc in a gridview with the background matching the paint color. Several links on the page are used to call itself with querystring values for brand, thinner, finish, etc. The page must show all colors if the querystring is empty and filter out records that don't match the querystring value when it's not. I've tried building...
0
1046
by: arulforum | last post by:
Actually i want to filter Eventlog Security source information based on username, Event,Computer,and Event ID wise. Actually this option is available on windows based drag and drop method but i need it to implement it using C# code in my project so help me to filter it.
5
3337
Jerry911
by: Jerry911 | last post by:
Hi, I have a query that I use to export data to a spreadsheet. The query itself works fine and I can manually edit the query to supply filtered information. What I would like to do is use a form that has several filter options so I do not have to modify my query every time I run it. I have a form with a list box to define one of the filters and I can make it work with that specific filter using the DoCmd.ApplyFilter function. I want/need to...
7
12512
by: DeZZar | last post by:
Hi all, Unfortunately I am quite a novice with Access!! I've created a number of data bases for my work however becuase my skills are limited to really built in functionality and wizards my programs are not really user friendly. I have searched and searched and tried numerous times to get the following right to no avail - and I think its really becuase of my lack of understanding.
5
2760
by: VictorG | last post by:
Hello, I am trying to secure a webservice using WSE 3.0 and the turnkey usernameForCertificateSecurity profile. I am passing a valid username token, and on the server I have overridden the Authenticate token call and it is being called. My ASP.NET service has a Login() method and it is being called during client application startup. Both the client and service have matching policy config files. Once authentication
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10624
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.