Eduardo
Eduardo

Reputation: 1837

Filtering EventLogs with C# by Date and Provider Name

I want to filter my Events Logs on Windows with C#. I'm using this code bellow, that works fine only if I have one condition in query.

When I try to input more that one condition, I get the error:

The specified query is invalid

string FormattedDateTime = string.Format("{0}-{1}-{2}T{3}:{4}:{5}.000000000Z",
                                          DateTime.Now.Year,
                                          DateTime.Now.Month.ToString("D2"),
                                          DateTime.Now.AddDays(-1).Day.ToString("D2"),
                                          DateTime.Now.Hour.ToString("D2"),
                                          DateTime.Now.Minute.ToString("D2"),
                                          DateTime.Now.Second.ToString("D2"));

query = String.Format("*[System/Provider/@Name=\"{0}\" and TimeCreated/@SystemTime>='{1}']", "MyApplication", FormattedDateTime);

var QueryResult = new EventLogQuery(LogSource, PathType.LogName, query);

So, the error occurrs in this line:

var Reader = new System.Diagnostics.Eventing.Reader.EventLogReader(QueryResult);

I've tried any differents combinations for this query, but without success.

Upvotes: 1

Views: 1500

Answers (0)

Related Questions